Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 4th, 2009, 10:48 AM
perelachess perelachess is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 1 perelachess User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 17 sec
Reputation Power: 0
Other Language - Help with AWK !!!

Hi all,
I am completely new to awk. I really need some help.
I want to write a script to suppress all the lines between "reference1" and "reference2"

I tried something like:

BEGIN {doprint=0}
{
match($0,"reference1") == 1 {doprint=0;};
match($0,"reference2") == 1 {doprint=1;} ;
if (doprint==1){print $0} }
}


without success.
Anyone can help ?


thanks a lot !!!!

Julien

Reply With Quote
  #2  
Old August 5th, 2009, 02:55 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
You can do this more easily with sed:

sed -n '/reference1/,/reference2/p' filename

The -n flag suppresses the default behaviour of printing every line.

/regex1/,regex2/p prints the lines from the one that matches regex1 up to the one that matches regex2.

Your awk program should also work I think, but is a bit verbose. You can simplify it to this:
Code:
/reference1/ {doprint = 1}
/reference2/ {doprint = 0}
{ if (doprint) {print $0}}

Variables are magically created when first used and initialised to 0 or "" depending on the context.
You can match lines using /regex/ instead of using the match function.

Dave

Reply With Quote
  #3  
Old August 24th, 2009, 06:09 PM
nathanpc's Avatar
nathanpc nathanpc is offline
PixHost.tk Developer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Location: Brazil
Posts: 58 nathanpc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 25 m 29 sec
Reputation Power: 0
Send a message via ICQ to nathanpc Send a message via AIM to nathanpc Send a message via MSN to nathanpc Send a message via Yahoo to nathanpc Send a message via Google Talk to nathanpc Send a message via Skype to nathanpc Send a message via XFire to nathanpc
Facebook Orkut
Exclamation

Just one other thing: put a better title in your questions, like what you really need and please put the code tags on your code.
Like this:
Code:
BEGIN {doprint=0}
{
match($0,"reference1") == 1 {doprint=0;};
match($0,"reference2") == 1 {doprint=1;} ;
if (doprint==1){print $0} }
}


Thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Other Language - Help with AWK !!!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap