Re: Extract a block of lines from a file



Christian wrote:
"Ed Morton" <morton@xxxxxxxxxxxxxx> a écrit dans le message de news: h_qdnRVpzfeAH1nbnZ2dnUVZ_qzinZ2d@xxxxxxxxxxxxxx

Christian wrote:

Hello,

I have a file named myfic.

root : venus - ~> cat myfic
the first line<LF>
the second line<LF>
the third line<LF>
<LF>
the fourth line<LF>
the fifth line<LF>
the sixth line<LF>
root : venus - ~>

The delimiter is the empty line. Here is what I do to extract the second block :

sed 's/^$/@@@@@@@/' myfic | awk 'BEGIN { RS = "@@@@@@@" }
{
if ( NR == 2 )
{
print $0
}
}'

Is there anything simpler using bash?

awk -v RS= 'NR==2' myfic

Ed.


Thanks, that works.

If your input file can be very large you may want to make the script more efficient, in which case change it to:

awk -v RS= 'NR==2{print;exit}' myfic

Regards,

Ed.
.



Relevant Pages