Why awk perform differently when using variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why awk perform differently when using variable?
# 1  
Old 01-20-2015
Why awk perform differently when using variable?

Hi Gurus,

I hit a hard block in my script.
when using awk command with variable, I got different result.
Please see below:
my test file as below:
Code:
$ cat demofile.txt
filename-yyyy-abcd
filename-xxx-week-pass
filename-xxx-week-run

for testing purpose, I put 3 awk command in one script.
Code:
$cat vartest.ksh
#!/bin/ksh

var=abcd
awk  '$0~/a/{print "ac", $0}!$0~/a/{print "su" ,$0}' a="$var" demofile.txt

echo "using -v "
/usr/xpg4/bin/awk -v a=$var '$0~/a/{print "ac", $0}!$0~/a/{print "su" ,$0}'  demofile.txt

echo "without variable"
awk  '$0~/abcd/{print "ac", $0}!$0~/abcd/{print "su" ,$0}' demofile.txt

the output as below:
Code:
$./vartest.ksh
ac filename-yyyy-abcd
ac filename-xxx-week-pass
ac filename-xxx-week-run
using -v
ac filename-yyyy-abcd
ac filename-xxx-week-pass
ac filename-xxx-week-run
without variable
ac filename-yyyy-abcd
su filename-xxx-week-pass
su filename-xxx-week-run

only 3rd command give me correct result.
my server is
Code:
uname -a
SunOS  5.10 Generic_150400-04 sun4v sparc sun4v

Thanks in advance.
# 2  
Old 01-20-2015
If /abcd/ is a literal string abcd,
then /a/ is a literal string a.
$0~a is the variable a.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 01-20-2015
Quote:
Originally Posted by MadeInGermany
If /abcd/ is a literal string abcd,
then /a/ is a literal string a.
$0~a is the variable a.
Thanks for your quick response.

I tried the script
I got 2nd and 3rd result. why 1st one gave me error?

Code:
$ cat vartest.ksh
#!/bin/ksh
var=abcd
awk  '$0~a {print "ac", $0} $0!~a {print "su" ,$0}' a="$var" demofile.txt
echo "using -v "
/usr/xpg4/bin/awk -v a=$var '$0~a{print "ac", $0} $0!~a{print "su" ,$0}'  demofile.txt
echo "without variable"
awk  '$0~/abcd/{print "ac", $0} !$0~/abcd/{print "su" ,$0}' demofile.txt

$ ./vartest.ksh
awk: syntax error near line 1
awk: bailing out near line 1
using -v
ac filename-yyyy-abcd
su filename-xxx-week-pass
su filename-xxx-week-run
without variable
ac filename-yyyy-abcd
su filename-xxx-week-pass
su filename-xxx-week-run

# 4  
Old 01-21-2015
Hello ken6503,

Could you please use change awk to /usr/xpg4/bin/awk or /usr/xpg6/bin/awk on a Solaris/SunOS system. This should fix the error:
Quote:
awk: syntax error near line 1
awk: bailing out near line 1

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-21-2015
Quote:
Originally Posted by RavinderSingh13
Hello ken6503,

Could you please use change awk to /usr/xpg4/bin/awk or /usr/xpg6/bin/awk on a Solaris/SunOS system. This should fix the error:



Thanks,
R. Singh
Hi R.Singh,

you are right. I need to use /usr/xpg4/bin/awk or nawk

thank you very much.
# 6  
Old 01-21-2015
One more thing, you apply the condition twice, one time positive, one time negative.
Consider changing to either
Code:
awk  '$0~a {print "ac", $0; next} {print "su" ,$0}' a="$var" demofile.txt

or
Code:
awk  '{if ($0~a) {print "ac", $0} else {print "su" ,$0}}' a="$var" demofile.txt

# 7  
Old 01-21-2015
Or
Code:
awk  'BEGIN{split("su ac", CM)} {print CM[($0~a)+1], $0}' a="$var" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script concatenate two column and perform mutiplication

Need your help in solving this puzzle. Any kind of help will be appreciated and link for any documents to read and learn and to deal with such scenarios would be helpful Concatenate column1 and column2 of file 1. Then check for the concatenated value in Column1 of File2. If found extract the... (14 Replies)
Discussion started by: as7951
14 Replies

2. Shell Programming and Scripting

awk treating variables differently in UNIX-Linux

Hi, awk seem to be acting differently in Unix and Linux when it comes to formatting. This is making it difficult to migrate scripts. for example: UNIX: echo "123" |awk '{printf ("%05s\n" ,$1)}' 00123 echo "123" |awk '{printf ("%05d\n" ,$1)}' 00123 echo "S12" |awk '{printf ("%05s\n"... (9 Replies)
Discussion started by: wanderingmind16
9 Replies

3. Shell Programming and Scripting

How to perform multiple operations on a number before storing to a variable?

(I am using bash) I have a command that will find the line number in a file by searching for a string where it exists. linenumber=$(grep -n "string" $FILENAME | cut -d : -fi) This returns the line number and removes the string. Now that I have the line number I want to subtract 4 from it and... (5 Replies)
Discussion started by: prodigious8
5 Replies

4. Shell Programming and Scripting

awk to perform splitting

Dear all, In my working directory I have two files as: $ ls -ltr -rw-r--r-- 1 emily af-cms 7 30. Jul 09:56 L1File1_000_981.root -rw-r--r-- 1 emily af-cms 10 30. Jul 09:56 L1File2_000_980.root I want to have the OutputFile.txt with the following content: L1File1_000_981.root... (6 Replies)
Discussion started by: emily
6 Replies

5. Shell Programming and Scripting

How to perform averaging of values for particular timestamp using awk or anythoing else??

I have a file of the form. 16:00:26,83.33 16:05:26,83.33 16:10:26,83.33 16:15:26,83.33 16:20:26,90.26 16:25:26,83.33 16:30:26,83.33 17:00:26,83.33 17:05:26,83.33 17:10:26,83.33 17:15:26,83.33 17:20:26,90.26 17:25:26,83.33 17:30:26,83.33 For the timestamp 16:00:00 to 16:55:00, I need to... (5 Replies)
Discussion started by: Saidul
5 Replies

6. Shell Programming and Scripting

How To Perform Mathematical Operation Within If in awk?

Hi All, I am using an awk script as below: awk -F'|' 'BEGIN{OFS="|";} { if ($1==$3 && $3==$7 && $7==$13 && $2==$6 && $6==$11 && $15-$14+1==$11) print $0"|""TRUE"; else print $0"|""FALSE"; }' tempfile.txt In above script, all conditions are being checked except the one which is... (4 Replies)
Discussion started by: angshuman
4 Replies

7. Shell Programming and Scripting

awk script modification - treat certain files differently

awk 'BEGIN{OFS=","} FNR == 1 {if (NR > 1) {print fn,fnr,nl} fn=FILENAME; fnr = 1; nl = 0} {fnr = FNR} /UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++} <'{system ("gunzip -cd FILENAME")}' END ... (2 Replies)
Discussion started by: SkySmart
2 Replies

8. Shell Programming and Scripting

perform echo and awk inside a string

hi, just wanted to make a shortcut of this one a="a b c" b=`echo $a | awk '{print $2}'` echo "the middle is $b" why can't i do this: a="a b c" echo "the middle is ${`echo $a | awk '{print $2}'`}" <- bad substitution :wall: thanks (6 Replies)
Discussion started by: h0ujun
6 Replies

9. Shell Programming and Scripting

Why is a variable behaving differently in ksh script.

Guys i have strange behaviour with command output being saved in a variable instead of a tmp file. 1. I suck command output into a variable Sample command output # cleanstats DRIVE INFO: ---------- Drv Type Mount Time Frequency Last Cleaned Comment *** ****... (1 Reply)
Discussion started by: lavascript
1 Replies

10. Windows & DOS: Issues & Discussions

Awk script in DOS and Linux behaves differently :(

Hi, I have an awk script which performs simple operations of variable assignments and finally printing the variables in custom form. BEGIN {FS=OFS="\n"} { v1=substr($0,1,15) v2=substr($0,16,200) v3=substr($0,216,20) print v1 "|" v2 "|" v3 } The input file being processed... (2 Replies)
Discussion started by: vidyak
2 Replies
Login or Register to Ask a Question