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 places everytime i deploy them to a new set of servers the awk-nawk problem should be automatically addressed.

Note: I cannot change my user .profile as it is used by other users as well.

Yes: use a variable (which holds the path to the respective awk). I suppose which one to use depends on the OS used at the respective server, so something along these lines:

typeset AWK="/this/is/the/fallback/awk"         # default location

# determine value for "$OS" here

case $OS in
     A)
          AWK="/usr/bin/awk"
          ;;

     B)
          AWK="/path/to/nawk"
          ;;

     Solaris)
          AWK="/usr/xpg4/bin/awk"
          ;;

     [...]
esac

[...]

$AWK 'here goes script1 ...' /path/to/file

[...]

$AWK 'script2 ...' /path/to/other.file

[...]

I hope this helps.

bakunin

@

While i understood that based on OS the AWK variable gets the value.

Did not understand how does the assigned AWK variable reflect in my scripts.

Did not understand the below part

[...]  
$AWK 'here goes script1 ...' /path/to/file  
[...]  
$AWK 'script2 ...' /path/to/other.file  
[...]

Usually I do

PATH=/usr/xpg4/bin:/bin:/usr/bin
awk '...'

That avoids the Solaris awk -> oawk.
And I avoid gawk-specifics, and even some Posix-compatible manipulations (like changing the # fields).

Basically you put the definition of $AWK at the beginning of all your scripts and then alter them such that any invocation of awk is

$AWK 'here goes script1 ...' /path/to/file

instead of

awk 'here goes script1 ...' /path/to/file

The

[...]

represents lines in your script up to and then between your awk invocations.

Alternatively, assuming you are running your scripts in a shell that uses aliases you could try:

[[ -x /usr/bin/gawk ]] && alias awk=/usr/bin/gawk
[[ -x /usr/bin/nawk ]] && alias awk=/usr/bin/nawk

putting the appropriate paths in reverse order of importance (i.e. if you want to use gawk if nawk does not exist the above code uses gawk, but if they both exist nawk is used instead); if nawk appears in different locations depending on the machine you can test for each location. You don't have to modify the rest of the script to invoke $AWK and awk is your default if nothing else exists.

And I only used gawk as an example; I realise you did not mention gawk in the original post.

Andrew

1 Like

@Andrew: I tried your suggestion for nawk instead of awk but it failed.

By now you should know the routine: what failed? Which way did it fail? Show us your script, show us the error message it produced, show us the return codes, log entries, diagnostic messages or whatever sheds light on the problem.

Finally: did you try what i told you? And - if it didn't work either - how did it not work (same as/see above)?

I don't want to threaten you but i am probably one of a (rapidly dwindling) group willing to even read, let alone answer, your threads. You show so little effort in even trying to understand what one tells you that it is frustrating. This way you have turned away most of the knowledgeable people writing here. They simply do not deem it worth their time trying to help a hopeless cause.

If you do not want to further reduce this group to zero and to avoid your threads being met by stony silence only you need to understand that your problem, regardless of us helping you or not, still remains your problem. You have to put effort (read: work and thinking) into finding a solution and that means at the very least to describe the problem as detailed as possible and to provide necessary information proactively instead of only after being asked. We are willing to recognize the intent and overlook deficiencies in the work but you don't even show the intent and this is what drives many of my friends here away. We may help you to help yourself but we are not your paid helpdesk. Furthermore we have no responsibilty to help you, we do it for fun - but in helping you there is no fun at all.

You may want to think about that.

bakunin

2 Likes

I would recommend a simpler and more portable solution:

PATH=$(getconf PATH):$PATH awk �

Should this need to work with the legacy Bourne shell on Solaris 10 and older:

PATH=`getconf PATH`:$PATH awk �

This should work on all Unix and Unix like OSes.

3 Likes

I've just discovered that my suggestion was flawed by the fact that bash only supports aliases when in interactive mode. You could try my suggestion of testing for nawk etc using the AWK variable as suggested by bakunin.

Just remember that where you would normally invoke awk or nawk in your scripts you use $AWK (or preferably ${AWK} ) instead.

Sorry about that.

Andrew