Solaris AWK still broken after all these years

December 8th, 2010

It’s amazing how time flies. I’ve got the above book sitting on my desk at work. It’s the only book on my desk at work. Its probably my favorite computer book for it’s terse clear style and simple power. I’ve keep it for about 20 years, and 20 years later, I’d expect any bugs in basic code examples to be worked out. For the most part on most platforms this is true, but not Solaris.
AWK is an awesome utility always available on UNIX and makes a perfect tool to for scripts that will run anywhere with no need for dependencies nor if’s, and’s or but’s. For that reason it’s all the more dissapointing that the default awk on Solaris is broken for syntaxes that were valid over 20 years ago. On the default awk on Solaris the following are broken
  1. awk -vVAR=value
  2. if ( $0 ~ “string” )
  3. gsub , sub

Probably the most useful thing to know is for point 1, passing in a shell variable, you can do it by setting a variable in the shell such as “VAR=10″ and accessing it in the awk script with:
Point 2, the if construct can be gotten around a bit less elegantly  with

 /string/   { ...

Point 3, gsub and sub are broken is just plain annoying and it takes writing your own routines with index and substr.
Which was too bad for me. I wanted to write a script to eliminate more than 2 redo logs from database creation for “scratch” databases. I don’t know if you can imagine a create script like:
STARTUP NOMOUNT pfile='/mnt/provision/redo2a/datafile/initredo2a.ora.provision'

) SIZE 52428800,

) SIZE 52428800,
) SIZE 52428800

I just wanted 2 redo logs and one member for group, so I wanted to pull out all that’s highlighted in orange. Simple with AWK:
cat ${MNTDIR}/ | \awk -vNREDO="$NREDO" 'BEGIN { output="on" members="off" nlogs=0 last="no"}{# membersif ( $0 ~ "GROUP"     ) { members="on" ; nlogs=0}# outputif ( $0 ~ NREDO       ) { last="yes" }if ( $0 ~ "DATAFILE"  ) { output="on"  }if ( $0 ~ "SIZE"      ) { members="off" ; nlogs=0}if ( output == "on"   ) {   nlogs++       gsub(/,/,"") } if ( nlogs < last ="=" last = "no" output="off"> /tmp/redo2.$$
The script works on AIX, HPUX, Redhat but not on Solaris. For Solaris I had a few choices. I could try and use nawk or gawk which would require me using some sort of “ifdef” and even then using nawk would require some changes. AFAIK gawk would have to be installed on the machines, thus ruling it out. For me, I wanted the safest option. The safest option for me was just to re-write the code in a way that worked around the bugs on Solaris default awk.
cat ${MNTDIR}/ | \awk  'BEGIN {output="on"members="off"nlogs=0last="no"size="no"NREDO="'"$NREDO"'"}/DATAFILE/               { output="on"  } # turn output back on/SIZE/                   { members="off" ; nlogs=0; size="yes"}  # end of group members/GROUP/                  { members="on"  ; nlogs=-1 }  # start counting group members{ if ( index($0,NREDO) )   last="yes"    } # reach the maximum  of redo logs to recreate{if ( output == "on"   ) {str=$0if ( members == "on"  ) { # we are in the log member section   nlogs++             # count members   i=index(str,",");   # get rid of comma at end of line   if ( i > 0 ) {       str=substr(str,0,i-1)   }}if ( nlogs < size ="=" last ="=" str="$0" i="index(str,"> 0 ) {                str=substr(str,0,i-1)            }            last = "no"            output="off"   # we reach max # of redo logs, turn output off till we see DATAFILE         }      }      print str   }}}{ size = "no" }'  > /tmp/redo2.$$

Kyle Hailey

  1. Trackbacks

  2. No trackbacks yet.

  2. No comments yet.