Saturday, May 9, 2009

Print next few lines after pattern - awk


Input data.txt is a collection report for XYZ corp group by different collection zones.

$ cat data.txt
Total Collection = $10291 {Fri May 8}
zone7 4500
zone8 3545
zone1 1200
zone0 900
zone3 70
zone5 67
zone11 9
Total Collection = $11847 {Sat May 9}
zone1 2800
zone3 2800
zone6 2567
zone8 2300
zone9 1200
zone12 90
zone11 90


Required: We need to find out the top 4 collection zones for each day from the above file. i.e. to print next 4 lines where the pattern "Total Collection =" is found (as the items are sorted on collection amount).
This is how we can achieve this using awk:

$ awk '/^Total Collection =/{c=4;next}c-->0' data.txt

zone7 4500
zone8 3545
zone1 1200
zone0 900
zone1 2800
zone3 2800
zone6 2567
zone8 2300


Now if we need to print the header line also, something like:

$ awk '/^Total Collection =/{c=4;{print}next}c-->0' data.txt

Total Collection = $10291 {Fri May 8}
zone7 4500
zone8 3545
zone1 1200
zone0 900
Total Collection = $11847 {Sat May 9}
zone1 2800
zone3 2800
zone6 2567
zone8 2300


And if you want to just print the date part as the header with top 4 collection zones.

$ awk -F "[{,}]" '/^Total Collection =/{c=4;{print $2}next}c-->0' data.txt

Fri May 8
zone7 4500
zone8 3545
zone1 1200
zone0 900
Sat May 9
zone1 2800
zone3 2800
zone6 2567
zone8 2300

As you can see we have used multiple field separator(FS) above to extract the date part from the header lines. A post on multiple FS with awk can be found here

Related posts:
- Print current,previous,next line where pattern is matched using awk and sed here
- Delete next few lines after pattern is found using sed here
- Print first line of each paragraph using awk here

2 comments:

Unknown said...

One more way:

$ awk '/pattern/{n=5;next}n{print;n--}' file

Krishna said...

Hi Jadu
This is Krishna, hope u remember me !

I have a simple requirement like this ... in the below file im expecting output
the contets in side
SQL> and SQL>
############EXPECTED OUT PUT#######################
rows will be truncated

rows will be truncated
NAME DATADB UUID
-------------------------------- -------------------------------- --------------
PV_SYSTEM PV 3efa89f8a62111
PV 3efa966ea62111
#################################################



My file looks like below
_______________FILE CNTENT ________________________________
SQL*Plus: Release 9.2.0.8.0 - Production on Wed Sep 15 15:04:14 2010

Connected to:
Oracle9i Enterprise Edition Release 9.2.0.8.0 - Production
With the Partitioning option
JServer Release 9.2.0.8.0 - Production

SQL> SQL> rows will be truncated

rows will be truncated

SQL>
NAME DATADB UUID
-------------------------------- -------------------------------- --------------
PV_SYSTEM PV 3efa89f8a62111
PV 3efa966ea62111

SQL> Disconnected from Oracle9i Enterprise Edition Release 9.2.0.8.0 - Production
_____________________________________________________________________________________

Please help me with this ..
I will be more happy if u reply me on my email krishnaspage@gmail.com
Thanks
Krishna

© Jadu Saikia www.UNIXCL.com