Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another way to print between two regular expressions 2

Status
Not open for further replies.

FedoEx

Technical User
Oct 7, 2008
49
US
Test file
Code:
$cat  testfile
a b c 
1 2  3 
begin  line with first regex
block     1  11
between   2  12 
regex     3  13
end  line with last regex
4 5 6 
blah blah

I need another method printing the block between two regular expressions
where the entire code is in “{ }” brackets.
I was thinking something like this code that prints the line containing the first regex.

Code:
 awk '{if(/begin/) print }' testfile

I can't use any statement outside the brackets like.
Code:
awk '/begin/,/end/{print $0}' testfile
Thanks.
 

Try this:
Code:
awk '/begin/{s=1} /end/{s=0} {if(s>1) print $0; s+=1}' testfile
[3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
The entire code needs to be enclosed in the "{....}" brackets.
Something like this incorrect expression.
Code:
 awk '{ if(/begin/,/end/) print $0  }' testfile
Thanks

 

Code:
awk '{if($1 ~ /begin/)s=1;if($1 ~ /end/)s=0;{if(s>1) print $0; s+=1}}' testfile
[tongue]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks. This almost works.
After the end regex the counter keeps increasing so the lines one after the regex gets printed out.
That is the line "blah blah" and after.
I can figure the fix for that.
Thanks again.
 
I am still looking for alternative way.
My idea is to get NR for the two regular expressions and then print for the range of lines where they are.
The code
Code:
awk '{
if($1~/begin/)begnr=NR;if($1~/end/)endnr=NR;
if(NR>3&&NR<7 ) print $0
}
END{ print begnr,endnr } ' testfile
gives me the desired output and I see that begnr,endnr was evaluated to 3,7 respectively.
However if I do
Code:
awk '{
if($1~/begin/)begnr=NR;if($1~/end/)endnr=NR;
if(NR>begnr&&NR<endnr ) print $0
}' testfile
I don't get any output.
I also tried
Code:
... if($1~/begin/)begnr=int(NR);if($1~/end/)endnr=int(NR);...

Any idea what might be wrong?
 

OK, I see...try this:
Code:
awk '{if($1 ~ /begin/)s=1;if($1 ~ /end/)e=1;{if(s>1&&e==0) print $0; s+=1}}' testfile
[thumbsup2]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
That one works.
Code:
 awk '{if($1 ~ /begin/)s=1;if($1 ~ /end/)s=-1000;{if(s>1) print $0; s+=1}}' testfile
where I set s=-1000 or some other big number which I know is always much larger than the number of lines between the two regex.
But still I can't really use it due to the specific of my actual problem.
What would really be the best is something without any counters. Like this.
Code:
 awk '{ if(/begin/,/end/) print $0  }' testfile

 
FedoEx, why do you have such strange restrictions on your code, out of curiosity?

Try:

Code:
awk '{ if ($1 ~ /begin/) { s=1; next; } if ($1 ~ /end/) s=0; if (s) print; }' testfile

Annihilannic.
 
Thanks.
To answer your question I need to open new thread.I will probably do that in a couple of days since I am pretty much stuck with the actual code.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top