How to pass IF condition via shell varibale in awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass IF condition via shell varibale in awk?
# 1  
Old 07-22-2016
How to pass IF condition via shell varibale in awk?

Hello,

I have one query regarding passing IF condition shell variable inside awk. Here is the case-

File content of keydefn.exp
Code:
201~2~LM Limit 02-current value~Limit 02    ~Limit02~Current~Value  ~N~Y~S~0~9999999
201~3~LM Limit 03-current value~Limit 03~Limit03~Current~Value~N~Y~S~-9999999~9999999

Case 1:
Code:
awk 'BEGIN{IGNORECASE=1;FS="~"}; { if ($2=="2") { print $0}}' keydefn.exp  ==> This is working fine!

Output:
Code:
201~2~LM Limit 02-current value~Limit 02    ~Limit02~Current~Value  ~N~Y~S~0~9999999


Case 2:
But when we move the IF condition in some variable like con='$2 == "2"' then it's not working

Code:
awk -v cond="$con" 'BEGIN{IGNORECASE=1;FS="~"}; { if (cond) { print $0}}' keydefn.exp  ==> This is not working

Output:
Code:
201~2~LM Limit 02-current value~Limit 02    ~Limit02~Current~Value  ~N~Y~S~0~9999999
201~3~LM Limit 03-current value~Limit 03~Limit03~Current~Value~N~Y~S~-9999999~9999999

Please suggest the reason!

Last edited by Scrutinizer; 07-22-2016 at 05:30 AM.. Reason: code tags
# 2  
Old 07-22-2016
I think it can not be done with -v option this way. Wat awk sees is a 9-character string $2 == "2" and it tests if it not null or an empty string. This is the case, so it will always evaluate to true. There is no eval mechanism like in the shell so that this somehow gets re-interpreted.

To see that it does not get evaluated: you get the same result with a condition that would normally lead to an error message (divide by zero):

Code:
$ cond='$27==1/0';  awk -v cond="$cond" 'BEGIN{IGNORECASE=1;FS="~"}; { if (cond) { print $0}}' infile 
201~2~LM Limit 02-current value~Limit 02    ~Limit02~Current~Value  ~N~Y~S~0~9999999
201~3~LM Limit 03-current value~Limit 03~Limit03~Current~Value~N~Y~S~-9999999~9999999

(The $-sign at the beginning of the lines is not part of the code and represents the command prompt)

Code:
$ awk 'BEGIN{IGNORECASE=1;FS="~"}; { if ($27==1/0) { print $0}}' infile
awk: division by zero
 input record number 1,


--
What you could do is pass a regex string, like so:
Code:
$ regex='^2$'; awk -v regex="$regex" 'BEGIN{IGNORECASE=1;FS="~"}; { if ($2~regex) { print $0}}' infile
201~2~LM Limit 02-current value~Limit 02    ~Limit02~Current~Value  ~N~Y~S~0~9999999

or a value:
Code:
$ val=2; awk -v val="$val" 'BEGIN{IGNORECASE=1;FS="~"}; { if ($2==val) { print $0}}' infile
201~2~LM Limit 02-current value~Limit 02    ~Limit02~Current~Value  ~N~Y~S~0~9999999

--
Or, do not use the -v option and let the shell put things in place before awk interprets:
Code:
$ cond='$2 == "2"'; awk 'BEGIN{IGNORECASE=1;FS="~"}; { if ('"$cond"') { print $0}}' infile
201~2~LM Limit 02-current value~Limit 02    ~Limit02~Current~Value  ~N~Y~S~0~9999999


Last edited by Scrutinizer; 07-22-2016 at 06:45 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-22-2016
What you could do - but this is far from comfortable nor is it bullet proof - is to analyze the variable's contents and react accordingly, like
Code:
awk -F~ -v cond="$con" '
BEGIN   {gsub (/[$"]/, _, cond) 
         split (cond, T, " ")
        }

T[2] == "==" && $T[1] == T[3] ||
T[2] == ">=" && $T[1] >= T[3] ||
T[2] == "<=" && $T[1] <= T[3] ||
T[2] == "!=" && $T[1] != T[3] ||
T[2] == ">"  && $T[1] >  T[3] ||
T[2] == "<"  && $T[1] <  T[3]           {print $0}
' file

With con='$2 == "2"' it yields
Code:
201~2~LM Limit 02-current value~Limit 02    ~Limit02~Current~Value  ~N~Y~S~0~9999999

, and with con='$2 >= "3"' it yields
Code:
201~3~LM Limit 03-current value~Limit 03~Limit03~Current~Value~N~Y~S~-9999999~9999999

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from awk script to shell?

Hi, Please need to print the Rej variable outsite the awk script which is given below...please advised how to achieve it. #!/bin/bash echo "Enter DMU Pipe delimited File name for the Feed to be validated" read DMU_File echo "Enter Pre-DMU File name for the Feed" read Predum_file ... (3 Replies)
Discussion started by: pelethangjam
3 Replies

2. Shell Programming and Scripting

Need to pass shell arguments into Nawk/awk

Hi, I am in critical need of help, Thanks a ton for your help. I need to know how to pass the shell argument into nawk code in AIX. so that my file gets passed into that awk script and it can execute it part. To be detail, i have more than 100 files and in those files a particular field... (6 Replies)
Discussion started by: Selva_2507
6 Replies

3. UNIX for Dummies Questions & Answers

Pass shell Variable to awk

Hello, May i please know how do i pass the shell variable to awk expression in the below script. It is returning null #!/bin/bash UNINUM=720922 UNINUM_DESC=`awk -F'|' -v UNINUM=$2 '/UNINUM/ {print $4}' datafile` echo $UNINUM_DESC datafile 4|First|720194|asdasdad 4|First|720735|asdasdsa... (8 Replies)
Discussion started by: Ariean
8 Replies

4. Shell Programming and Scripting

Pass awk array variable to shell

Hi, all suppose I have following myfile (delimited by tab) aa bb cc dd ee ffand I have following awk command: awk 'BEGIN{FS="\t"}{AwkArrayVar_1=$1;AwkArrayVar_2=$2};END{for(i=0; i<NR; i++) print i, AwkArrayVar_1, AwkArrayVar_2,}' myfileMy question is: how can I assign the awk array... (7 Replies)
Discussion started by: littlewenwen
7 Replies

5. Shell Programming and Scripting

pass shell parameters to awk does not work

Why does this work for myfile in `find . -name "R*VER" -mtime +1` do SHELLVAR=`grep ^err $myfile || echo "No error"` ECHO $SHELLVAR done and outputs No error err ->BIST Login Fail 3922 err No error err ->IR Remote Key 1 3310 err But... (2 Replies)
Discussion started by: alan
2 Replies

6. UNIX for Dummies Questions & Answers

How to pass a variable from shell to awk

I know this topic has been dealt with previously, but the solutions I've seen don't work for me apparently. I need to pass a variable defined in the shell to one in awk: $ echo $var1 3 $ cat aaa aaa 1 bbb 2 ccc 3 ddd 4 eee 5I've tried this, without success: $ awk... (2 Replies)
Discussion started by: metaltree
2 Replies

7. Shell Programming and Scripting

How to pass shell variables to awk's pattern?

How would I get folders owned by specific users.. I want to pass users as a shell variable to awk. drwxr-x--x 3 user1 allusers 512 Oct 14 2006 946157019/ drwxr-x--x 3 user2 allusers 512 Mar 9 2008 94825883/ drwxr-x--x 3 user3 allusers 512 Mar 9 2008 948390501/ ... (3 Replies)
Discussion started by: kchinnam
3 Replies

8. Shell Programming and Scripting

How to pass values between awk and shell scripts

I know that we can call system command to execute shell script in awk. but it does not return the result of the command executed , but only returns the value of the command executoin status ( 1/0 --> failure / success). Could anyone let me know how to solve this problem. (9 Replies)
Discussion started by: rajnikanth.1912
9 Replies

9. Shell Programming and Scripting

Pass array variabel to awk from shell

Hi I need to pass an array to Awk script from Shell. Can you please tell how to do it? How to pass this array add_ct_arr to an awk script or access it in awk? i=1 while ; do add_ct_arr=$(echo ${adda_count} | awk -v i=$i -F" " '{print $i;}') echo ${add_ct_arr} ... (1 Reply)
Discussion started by: appsguy616
1 Replies

10. Shell Programming and Scripting

Is it possible to pass variable from awk to shell script

Hello experts, can I return a value from gawk to a shell script ? My script as follows, #Here I want the num value to shell script so that I can use later gawk ' { split($0,num,","); print num }' gawk -v no=$number '{print no}' file1 ... (3 Replies)
Discussion started by: user_prady
3 Replies
Login or Register to Ask a Question