Operating System - HP-UX
1834465 Members
3247 Online
110067 Solutions
New Discussion

AWK: 1st line to blank line, plus 4

 
SOLVED
Go to solution
Robert A. Pierce
Frequent Advisor

AWK: 1st line to blank line, plus 4

Good day!

I have a short awk script that prints the first line to the first blank line:

awk 'NR==1,/^$/' $1

How could I make this print to the first blank line, -plus- four lines?

e.g. in file "test"

"firstline
stuff
also stuff

first after break
foo
bar
fourth
for bridge
also also stuff"

I'd like to print from "firstline" to "fourth."

Thanks,

Rob Pierce
9 REPLIES 9
H.Merijn Brand (procura
Honored Contributor
Solution

Re: AWK: 1st line to blank line, plus 4

Easy in perl. Might be a hint for awk

# perl -ne'(1../^$/)||$n++<4 and print' test

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
curt larson_1
Honored Contributor

Re: AWK: 1st line to blank line, plus 4

awk '{
print $0;
if ( $0 ~ /^$/ ) {
for ( i=1;i<4;i++) {
getline;print $0;
}
exit;
}}'
Robert A. Pierce
Frequent Advisor

Re: AWK: 1st line to blank line, plus 4

Thanks a lot, both of you.

Works like a champ!
H.Merijn Brand (procura
Honored Contributor

Re: AWK: 1st line to blank line, plus 4

curt, shouldn't that be

awk '{print $0}
$0 ~ /^$/ {
for (i = 1; i < 4; i++) {
getline; print $0;
exit;
}}'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: AWK: 1st line to blank line, plus 4

No, it should not. brace mismatch in /my/ head. No points please.

awk '{print $0}
$0 ~ /^$/ {
for (i = 1; i < 4; i++) {
getline; print $0;
}
exit;
}'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
curt larson_1
Honored Contributor

Re: AWK: 1st line to blank line, plus 4

procura,

either way will work. as will

awk '
NR==1,/^$/ {$1}
/^$/ {
for (i = 1; i < 4; i++) {
getline; print $0;
exit;}
}'
H.Merijn Brand (procura
Honored Contributor

Re: AWK: 1st line to blank line, plus 4

Now as one-liners:

lt09:/tmp 110 > awk '{print}/^$/{for(i=1;i<=4;i++){getline;print}exit}' xx
kwerhqlre
serths
serths
serths
serths
yh

rharthrtharthh
serths
hsdrhare
dthsrh
lt09:/tmp 111 > perl -ne'(1../^$/)||$n++<4 and print' xx
kwerhqlre
serths
serths
serths
serths
yh

rharthrtharthh
serths
hsdrhare
dthsrh
lt09:/tmp 112 >

BUT!! What if there are not 4 lines after the empty line:

lt09:/tmp 112 > awk '{print}/^$/{for(i=1;i<=4;i++){getline;print}exit}' xx
kwerhqlre
serths
serths
serths
serths
yh

rharthrtharthh
rharthrtharthh
rharthrtharthh
rharthrtharthh
lt09:/tmp 113 > perl -ne'(1../^$/)||$n++<4 and print' xx
kwerhqlre
serths
serths
serths
serths
yh

rharthrtharthh
lt09:/tmp 114 >

See, the perl version is safe here, the awk version is not.

Feel free to leave this pointless. It's to make up for my miss :)

Enjoy, Have FUN! H.Merijn [ testing his awk skills again ]
Enjoy, Have FUN! H.Merijn
Francisco J. Soler
Honored Contributor

Re: AWK: 1st line to blank line, plus 4

Hi Rob,

Here my awk without the getline command:

awk '
/^$/ {flag=1 ; count=0}
{
if (! flag || count<5){
print
count++
}
}' test


Frank.
Linux?. Yes, of course.
Hein van den Heuvel
Honored Contributor

Re: AWK: 1st line to blank line, plus 4


Just being silly and pointless ...

Here my awk without everything.
Look ma.. no hands.
Still works though:

awk '{p+=x}/^$/{x=1}(p<5)' test

It combines the count and flag from Frank's example. A counter p is incremented by x where x starts out null. Ones an empty line is seen, x becomes one and the counter really starts. It prints through a default { print } action on any pattern test. That pettern is to see wether the counter exceeded the target.
I would NOT want to find this in production code. Impossible to explain!

Hein.