Friday, May 8, 2009

Print first line of each paragraph - awk


Input file:

$ cat file.txt
S Badrinath
c Singh b Pathan
0

ML Hayden
c Jayawardene b Sreesanth
89
6X6

SK Raina
c Goel b Chawla
32
1X6

Required: Print the first line of each paragraph (each paragraph being separated by a single new line)
Awk solution:

$ awk 'NR==1||cnt-->0;/^$/{cnt=1}' file.txt

Output:

S Badrinath
ML Hayden
SK Raina

Related post:
- Make 4 line paragraph using awk here
- Make 4 line paragraph using Linux paste command here

1 comment:

V said...

The command tries to find empty lines and then prints the line after that. Is that correct?

I have written some examples. If you are interested, you can check this: Awk Command

© Jadu Saikia www.UNIXCL.com