Problem
Shell scripts using awk, which have worked correctly in versions
of SCO UNIX prior to SCO OpenServer 5.0.4, are now producing
different results. In particular, the first value returned is
sometimes blank rather than the expected value. This can be
demonstrated from the command line as follows:%cat /etc/passwd | grep root | awk '{ FS=":" ; print $5 }'This has always returned Superuser in previous revisions, but now it returns a blank line.
Cause
The problem is now occurring because of modifications made to awk
in order to make it XPG4 compliant. In the example above, the
change of FS is in the "action" which is executed after the first
record (line) is read. This becomes clearer if the example above
is modified to return the names of all users in the password file
as follows:%cat /etc/passwd | awk '{ FS=":" ; print $5 }'All names are printed except for the first entry which appears blank.
Solution
The awk string should be rewritten to ensure that the field separator is defined before the first record is processed. This can be done by either using the -F command line option or making use of the BEGIN action. For the example above, this can be rewritten as follows:
%cat /etc/passwd | awk -F: '{ print $5}'or:%cat /etc/passwd | awk 'BEGIN {FS = ":"} {print $5}'In both of these examples, all user names are returned.