bash-3.2$ /usr/bin/nawk '/<server>/{A=1;++i} A{print >> ("manages"i".tmp")} /<\/server>/{A=0}' config.xml
bash-3.2$ /usr/bin/awk '/<server>/{A=1;++i} A{print >> ("manages"i".tmp")} /<\/server>/{A=0}' config.xml
awk: syntax error near line 1
awk: bailing out near line 1
bash-3.2$ uname -a
SunOS mymac 5.10 Generic_150400-40 sun4v sparc sun4v
Can you tell me why awk fails when nawk does not ?
As part of a more complex requirement I need a variable AWK to change between awk and nawk like you can see below is the sourced file.
[[ -x /usr/bin/gawk ]] && AWK=/usr/bin/gawk
[[ -x /usr/bin/nawk ]] && AWK=/usr/bin/nawk
[[ -x /usr/bin/awk ]] && AWK=/usr/bin/awk
However the problem i am reporting renders this useless as despite awk being present and chosen fails due to the shown error.
Sample config.xml
<server>
<name>adminServer</name>
<ssl>
<name>adminServer</name>
<listen-port>7002</listen-port>
</ssl>
<listen-port>7001</listen-port>
<listen-address>108.66.55.2</listen-address>
</server>
You should not use awk on Solaris, which is only there for legacy reasons. On Solaris use /usr/xpg4/bin/awk or nawk, rather than awk.
I think they had that reversed, IMO that should have been
[[ -x /usr/bin/awk ]] && AWK=/usr/bin/awk
[[ -x /usr/bin/nawk ]] && AWK=/usr/bin/nawk
[[ -x /usr/bin/gawk ]] && AWK=/usr/bin/gawk
Why don't you turn that into:
if [[ -x /usr/bin/gawk ]]; then
AWK=/usr/bin/gawk
elif [[ -x /usr/bin/nawk ]]; then
AWK=/usr/bin/nawk
elif [[ -x /usr/bin/awk ]]; then
AWK=/usr/bin/awk
fi
And perhaps also throw /usr/xpg4/bin/awk in the mix..
You can also change the search PATH to something like:
export PATH=/usr/xpg4/bin:/bin:/usr/bin
at the start of the script and just use awk in your script ..
Just add all the paths to the executables that you need in your script in the right order ..
Hi,
In terms of a solution for picking one (and only one) version of awk to use, the problem here is you're doing three checks that are un-related to each other, and (as you've found) if multiple awks are found then you can end up with undesirable behaviour or the 'wrong' awk being picked.
The best thing to do here I think would be to use an if/elif/else kind of decision process, so that based on your order of precedence you pick one (and only one) version of Awk at all times.
For instance, if your order of preferred awk implementations is:
- gawk
- nawk
- awk
then the following code would ensure that one, and only one, was picked (and it would be the first one found, in order of precedence):
if [ -x /usr/bin/gawk ]
then
AWK=/usr/bin/gawk
elif [ -x /usr/bin/nawk ]
then
AWK=/usr/bin/nawk
else
AWK=/usr/bin/awk
fi
So even if your system has both gawk and nawk , it will always pick gawk and leave it at that.
Hope this helps.