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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get matched lines + lines after the matched lines till empty line

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL
example:

#-begin text file
====
adate
1111

bleble
ble
====
adate
2222

ble
ble

====
adate

ble
====
adate
5
5

ble
#-end text file

'grep'ing for "====" expected result is:

====
adate
1111
====
adate
2222
====
adate
====
adate
5
5


thx in adv.!
 
...besides for each matched line numer of lines after this matched line is not the same
 
You might also try the grep -p (paragraph). Only thing is, you get the whole paragraph (also the lines preceding your match).

awk can be helpful:

# awk '/\=\=\=\=/,/^$/' file

but you get the empty line also...

HTH,

p5wizard
 
Thank you but I made a mistake - after the "adate" there is empty line so the expected result is:

====
adate

1111
====
adate

2222
====
adate

====
adate

5
5


in the other words i need grep from matched line till second occurence of empty line after that matched line
 
Code:
perl -n -e 'print if ( /adate/ .. /^$/ );' file.txt

--
 
or with awk:

awk '{
if ($0 == "====") {flag=2}
if ($0 == "") {flag--}
if (flag > 0) {print}
}' file


HTH,

p5wizard
 
or if the starting RE is embedded in other text:

awk '{
if ($0 ~ "====") {flag=2}
if ($0 == "") {flag--}
if (flag > 0) {print}
}' file


HTH,

p5wizard
 
Or perhaps this ?
awk '/^====/{nr=3+NR}NR<=nr' /path/to/textfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top