Why does awk fail when nawk does not?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why does awk fail when nawk does not?
# 1  
Old 04-06-2017
Oracle Why does awk fail when nawk does not?

Code:
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.
Code:
[[ -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
Code:
  <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>


Last edited by Scrutinizer; 04-06-2017 at 11:17 AM.. Reason: quote tags => code tags
# 2  
Old 04-06-2017
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
Code:
[[ -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:

Code:
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:
Code:
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 ..

Last edited by Scrutinizer; 04-06-2017 at 11:27 AM..
# 3  
Old 04-06-2017
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:
  1. gawk
  2. nawk
  3. 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):

Code:
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.
This User Gave Thanks to drysdalk For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to detect awk and nawk?

I have 10 scripts which use awk/nawk extensively. I have to always change awk to nawk and nawk to awk when i deploy all my scripts to different types of servers as some support nawk while other support awk. Can you propose a solution that without having to tweak my 10 scripts at several... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Awk/Nawk Questions

Hi Guys, This is the Input: <xn:MeContext id="XXX012"> <xn:ManagedElement id="1"> <xn:attributes> <xn:userLabel>XXX012</xn:userLabel> <xn:swVersion>R58E68</xn:swVersion> </xn:attributes> </xn:ManagedElement> </xn:MeContext>... (4 Replies)
Discussion started by: smarones
4 Replies

3. Shell Programming and Scripting

Awk,nawk Help please

Hi Guys, I am in need of some help; I have an xml message file which contains personal details as shown below: , message=, message=, message=, message=, message=, message= I want to use nawk to parse these xml messages but I am new to awk and nawk. What I want is to get output... (7 Replies)
Discussion started by: James_Owen
7 Replies

4. UNIX for Dummies Questions & Answers

Help me to know about awk and nawk

Hi everyone, i am new to unix , so i want to know what is the use of awk and nawk. because in most of the place this cmds were used. so, if anyone provied the basic idea of this cmds, it will be much helpfull for me . . .. Thnks in Advance :) (9 Replies)
Discussion started by: natraj005
9 Replies

5. Shell Programming and Scripting

comparing awk and nawk

Hi Guys, i tried these two commands. First in awk and nawk. The nawk command is running fine but the awk command is throwing error. What is wrong with the awk command. There are lot of awk commands running fine in my system d003:/usr/local/dsadm/dsprod>nawk 'NR = 1 {print " "$0}' a.txt ... (6 Replies)
Discussion started by: mac4rfree
6 Replies

6. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

7. UNIX for Dummies Questions & Answers

How to use awk instead of nawk?

Hi all, I can run the following script using nawk..However, I find that teh server dun support nawk.. May I know how to change teh script to use awk such that it will work? Very urgent.. thx! nawk 'BEGIN {FS=OFS=","} NR==FNR{arr=$2;next} $0 !~ "Documentation"{print $0;next} ... (2 Replies)
Discussion started by: kinmak
2 Replies

8. Shell Programming and Scripting

nawk -v to awk

hi, i have the command nawk -v i want to use it equivalent in awk? any help please :) (2 Replies)
Discussion started by: kamel.seg
2 Replies

9. UNIX for Dummies Questions & Answers

help with Awk or nawk

Can anyone explain to me why the first line doesn't work and the second seems to work fine. I am trying to find all occurances of text within a certain column (col 13) that start with the character V, I suppose it sounds simple but I have tried using the following but don't really understand what... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question