I had a thread on this a couple of years ago, but here's the updated
version since I just stumbled across a new issue (case 16 below).
Below are some reasons why /usr/bin/awk on Solaris is referred to as
old, broken awk and should not be used. If on Solaris use GNU awk
(gawk), /usr/xpg4/bin/awk or New awk (nawk) instead.
If anyone has additional examples of things that are missing from and/or
broken in /usr/bin/awk feel free to post a followup but keep each point
brief.
Ed.
1) no conditions without explicit comparisons:
$ echo "foo" | /usr/xpg4/bin/awk '1{print}'
foo
$ echo "foo" | /usr/bin/awk '1==1{print}'
foo
$ echo "foo" | /usr/bin/awk '1{print}'
awk: syntax error near line 1
awk: bailing out near line 1
$ echo "foo" | /usr/bin/awk '1==1{print}'
foo
$ echo "foo" | /usr/xpg4/bin/awk '!x{print}'
foo
$ echo "foo" | /usr/bin/awk '!x{print}'
awk: syntax error near line 1
awk: bailing out near line 1
foo
$ echo "foo" | /usr/bin/awk 'x!=0{print}'
foo
2) no ternary expressions:
$ echo "foo" | /usr/xpg4/bin/awk '{print ($0=="foo") ? "yes" : "no"}'
yes
$ echo "foo" | /usr/bin/awk '{print ($0=="foo") ? "yes" : "no"}'
awk: syntax error near line 1
awk: illegal statement near line 1
3) no ARGC:
$ echo "foo" | /usr/xpg4/bin/awk '{print "<"ARGC">"}'
<1>
$ echo "foo" | /usr/bin/awk '{print "<"ARGC">"}'
<>
4) no sub()/gsub():
$ echo "foo" | /usr/xpg4/bin/awk '{print "<" sub(/o/,"X") ">"}'
<1>
$ echo "foo" | /usr/bin/awk '{print "<" sub(/o/,"X") ">"}'
awk: syntax error near line 1
awk: illegal statement near line 1
5) no -v:
$ echo "foo" | /usr/xpg4/bin/awk -v var="X" '{print "<" $0,var ">"}'
<foo X>
$ echo "foo" | /usr/bin/awk -v var="X" '{print "<" $0,var ">"}'
awk: syntax error near line 1
awk: bailing out near line 1
6) inexplicable parameter parsing (here thinks "<bar>" is arg to sprintf()):
$ echo "foo" | /usr/xpg4/bin/awk '{print sprintf("%s =",$0), "<bar>"}'
foo = <bar>
$ echo "foo" | /usr/bin/awk '{print sprintf("%s =",$0), "<bar>"}'
foo =
7) no functions:
$ echo "foo" | /usr/xpg4/bin/awk 'function bar(){print} {bar()}'
foo
$ echo "foo" | /usr/bin/awk 'function bar(){print} {bar()}'
awk: syntax error near line 1
awk: bailing out near line 1
8) no dynamic REs:
$ echo "foo" | /usr/xpg4/bin/awk 'BEGIN{re="o"} $0 ~ re {print}'
foo
$ echo "foo" | /usr/bin/awk 'BEGIN{re="o"} $0 ~ re {print}'
awk: syntax error near line 1
awk: bailing out near line 1
9) Hangs if no input and no explicit exit:
$ time /usr/xpg4/bin/awk 'BEGIN{print "begin"}'
begin
real 0m0.06s
user 0m0.01s
sys 0m0.02s
$ time /usr/bin/awk 'BEGIN{print "begin"}'
begin
^?$
10) Can't delete an array element:
$ /usr/xpg4/bin/awk 'BEGIN{ a[1]=a[2]; delete a[1]; for (i in a) print
i; exit }'
2
$ /usr/bin/awk 'BEGIN{ a[1]=a[2]; delete a[1]; for (i in a) print i;
exit }'
2
1
11) Can't test for an element being "in" an array:
$ /usr/xpg4/bin/awk 'BEGIN{ a[1]=7; if (1 in a) print a[1]; exit }'
7
$ /usr/bin/awk 'BEGIN{ a[1]=7; if (1 in a) print a[1]; exit }'
awk: syntax error near line 1
awk: illegal statement near line 1
12) No FNR variable:
$ cat file1
a
$ cat file2
b
$ /usr/xpg4/bin/awk 'NR==FNR{print}' file1 file2
a
$ /usr/bin/awk 'NR==FNR{print}' file1 file2
13) No match() function:
$ /usr/xpg4/bin/awk '{print match($0,"a")}' file1 file2
1
0
$ /usr/bin/awk '{print match($0,"a")}' file1 file2
awk: syntax error near line 1
awk: illegal statement near line 1
14) FS cannot be an RE:
$ echo "faobo" | /usr/xpg4/bin/awk '{ print $1,$2,$3 }' FS="[ab]"
f o o
$ echo "faobo" | /usr/bin/awk '{ print $1,$2,$3 }' FS="[ab]"
faobo
15) Cannot set $0:
$ /usr/xpg4/bin/awk 'BEGIN{$0="foo"; print}'
foo
$ /usr/bin/awk 'BEGIN{$0="foo"; print}'
awk: can't set $0
16) Strings can unexpectedly become numeric variables:
$ /usr/xpg4/bin/awk 'BEGIN{ c=3; print "c=" ++c; exit }'
c=4
$ /usr/bin/awk 'BEGIN{ c=3; print "c=" ++c; exit }'
03
$ /usr/xpg4/bin/awk 'BEGIN{ c=3; print "c=" ++c; print ++"c=" c; exit }'
/usr/xpg4/bin/awk: syntax error Context is:
>>> BEGIN{ c=3; print "c=" ++c; print ++"c=" <<<
$ /usr/bin/awk 'BEGIN{ c=3; print "c=" ++c; print ++"c=" c; exit }'
03
23
Posted using
www.webuse.net