Operating System - HP-UX
1834216 Members
2571 Online
110066 Solutions
New Discussion

Re: Extract a paragraph using sed

 
SOLVED
Go to solution
YLTan
Frequent Advisor

Extract a paragraph using sed

Hi

Does any have some good tips to share for extracting any tagged paragraph of 10 lines from a text file using sed?? e.g.


lines...
lines...
lines...


lines...
lines...
lines...


lines...
lines...
lines...
tyl
5 REPLIES 5
Jean-Louis Phelix
Honored Contributor

Re: Extract a paragraph using sed

Hi,

I don't really think that sed is the best tool to do this and also I'm not really sure to understand what output you need. But this script should display and following 3 lines (I'm not very proud of it ...).

Regards.

input file is :


a1
a2
a3
QWERTYUIOP
dummy lines ...

b1
b2
b3

Sed command would be :

sed -n '// {
p
n
p
n
p
n
p
}' file

Output would b :


a1
a2
a3

b1
b2
b3
It works for me (© Bill McNAMARA ...)
Bill McNAMARA_1
Honored Contributor

Re: Extract a paragraph using sed

maybe this link with awk will help!

It works for me (tm)
Christian Gebhardt
Honored Contributor

Re: Extract a paragraph using sed

Hi

try this

awk '{ line[NR]=$0 }
END {
number=10
for (i=0 ; i <=NR ; i++) {
if ( line[i] ~ // ) {
for (ii=i ; ii <=i+number ; ii++) {
print line[ii]
}
}
}
}
' inputfile


Chris
Bill McNAMARA_1
Honored Contributor

Re: Extract a paragraph using sed

yikes! forgot the link!!

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xf6ca0bce6f33d6118fff0090279cd0f9,00.html

Good luck,
Bill
It works for me (tm)
Pete Randall
Outstanding Contributor
Solution

Re: Extract a paragraph using sed

Here's the infamous "Handy One-Liners for Sed":


Pete

Pete