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!

Posted by matty, filed under UNIX Shell. Date: October 29, 2005, 8:01 pm |

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.