Sectioning data with awk


I was working on a shell script last week and wanted to grab just the CPU section from the Solaris prtdiag(1m) output. I was able to perform this operation with awk by checking $0 for one or more “=” characters, and then setting a variable named SECTION to the value contained in the second position variable. If this variable was equal to the string CPUs, all subsequent lines would be printed up until the next block of “=” characters were detected. The awk script looked similar to the following:

$ prtdiag -v | awk '$1 ~ /^\=+/ {SECTION=$2} { if ($SECTION == "CPUs") print }'

==================================== CPUs ====================================
E$ CPU CPU
CPU Freq Size Implementation Mask Status Location
--- -------- ---------- ------------------- ----- ------ --------
0 502 MHz 256KB SUNW,UltraSPARC-IIe 1.4 on-line +-board/cpu0

I really dig awk!

This article was posted by Matty on 2005-10-29 20:01:00 -0400 EDT