awk works on Linux but fails on Solaris

On linux i have the below command working fine.

awk '/<app-deploy>/{A=1;++i} /<\/app-deploy>/{print >> "found"i".tmp";A=0} A{;print >> "found"i".tmp"}' deploy.xml

But the same is failing on Solaris
Output:

awk: syntax error near line 1
awk: bailing out near line 1
uname -a SunOS mymac 5.10 Generic_150400-23 sun4v sparc sun4v

Can anyone help fix the code.

Use nawk on Solaris.

1 Like

Use nawk instead /usr/bin/awk is there for compatibility with ancient shell scripts.

Solaris has several sets of "commands under the /usr directory:
/usr/ucb, /usr/bin, /usr/xpg4/bin and so on.

You have to be aware of which set of commands you are using in your aliases or in your PATH on Solaris when you port code to there.

As has been stated in hundreds of threads before, when running awk scripts on Solaris/SunOS systems, change awk to /usr/xpg4/bin/awk or nawk . (And, if there are any internationalized EREs, don't use nawk either. But, that isn't a problem in this script.)

But, your code is also depending on something else that is unspecified in the standards: the standards don't specify the precedence of file redirection versus string concatenation. So, to make your script more portable, use:

awk '/<app-deploy>/{A=1;++i} /<\/app-deploy>/{print >> ("found"i".tmp");A=0} A{print >> ("found"i".tmp")}' deploy.xml

But, you might want to consider simplifying it a little bit to:

awk '/<app-deploy>/{A=1;++i} A{print >> ("found"i".tmp")} /<\/app-deploy>/{A=0} ' deploy.xml

Then when running it on Solaris systems, change awk to /usr/xpg4/bin/awk or nawk .

1 Like

NAWK gives me the below error.

nawk: syntax error at source line 1
 context is
        /<app-deploy>/{A=1;++i} /<\/app-deploy>/{print >> >>>  "found"i <<< ".tmp";A=0} A{;print >> "found"i".tmp"}
nawk: illegal statement at source line 1

Have you looked at post #4 in this thread???