How to detect awk and nawk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to detect awk and nawk?
# 1  
Old 01-12-2017
Lightbulb 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.

Last edited by mohtashims; 01-12-2017 at 05:39 PM..
# 2  
Old 01-12-2017
Quote:
Originally Posted by mohtashims
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.
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:

Code:
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
# 3  
Old 01-12-2017
Lightbulb

@

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

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

# 4  
Old 01-12-2017
Usually I do
Code:
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).
# 5  
Old 01-13-2017
Quote:
Originally Posted by mohtashims
@

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

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

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
Code:
$AWK 'here goes script1 ...' /path/to/file

instead of
Code:
awk 'here goes script1 ...' /path/to/file

The
Code:
[...]

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:
Code:
[[ -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
This User Gave Thanks to apmcd47 For This Post:
# 6  
Old 01-19-2017
Hammer & Screwdriver

Quote:
Originally Posted by apmcd47
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
Code:
$AWK 'here goes script1 ...' /path/to/file

instead of
Code:
awk 'here goes script1 ...' /path/to/file

The
Code:
[...]

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:
Code:
[[ -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
@Andrew: I tried your suggestion for nawk instead of awk but it failed.

Last edited by mohtashims; 01-19-2017 at 01:47 PM..
# 7  
Old 01-19-2017
Quote:
Originally Posted by mohtashims
@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
These 2 Users Gave Thanks to bakunin 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 empty field in awk ?

Hi ! programmers I have a need of detecting empty field in file my file looks like this 40.900|-71.600|1.6|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|5.7|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|7.8|20|1|1961|21.00|3.700||1|US|28035|10029370|31... (7 Replies)
Discussion started by: Dona Clara
7 Replies

2. 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

3. 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

4. Shell Programming and Scripting

AWK script to detect webpages from file

Hi guys I'm very new to unix and I have to create an awk script that detects webpage addresses from a file/webpage and outputs how many times each webpage was detected.e.g. if my file was: www.google.com www.facebook.com www.google.com the output should be: www.google.com x2... (2 Replies)
Discussion started by: ROFL
2 Replies

5. Shell Programming and Scripting

AWK script to detect webpages from file

Hi guys I'm very new to unix and I have to create an awk script that detects webpage addresses from a file/webpage and outputs how many times each webpage was detected.e.g. if my file was: (Note: The symbol " was added to stop them being created into links) "www.google.com"... (1 Reply)
Discussion started by: ROFL
1 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