Variable IF statement in awk

I have multiple requirements to run data extracts (with the same basic requriement / resulsts) however the parameters of the extracts change with each request. To help make life easier I am writing a script where the parameters for the specific request are set at the start and then used as needed.
HOWEVER I am having problems with the awk portion of the script.

To simplify matters here is test data and script to explain the problem.

data file as follows (test)

1 a
2 b
3 c
4 d

in this instance I want to output record 3 c

script as follows:

parm='&& ($2=="c")';
cat test | awk -v parm="$parm" '{if ((1==1) parm) {print $0}}'

this is outputting the entire file and not just the 3rd record
(note: the (1==1) is a failsafe incase there is no parameter passed)

Why you need a fail-safe? How about:

parm="c"

awk -v p=$parm '{
 if(p=="") { print $0; }        # If p is not defined print all records in file.
 else { if($2==p) print $0 }    # If p is defined, then print only that specific record.
}' file

Also note that the cat command is useless, awk is capable of reading the file by itself.

hi bipinajith
my problem is that the filter field changes with each request. next time it could be column 1 or column 10. I need the whole statement to be flexible.

Ok, then how about using a variable for column:-

col=2
parm="c"

awk -v c=$col -v p=$parm '{if(c=="") { print $0; }else { if($c==p) print $0 }}' file

[quote=bipinajith;302754221]
Why you need a fail-safe? How about:

parm="c"

awk -v p=$parm ...
[...]

As a side note:
 - in most cases it's safer to quote $param:, just in case the value of $param contains spaces or other shell special characters 
and shell word/field splitting is enabled (default for most shells, excluding zsh):

$ param='a b'
$ # this one fails
$ awk -v p=$param 1 < /dev/null
awk: cmd. line:1: fatal: cannot open file `1' for reading (No such file or directory)
$ # this one succeeds 
$ awk -v p="$param" 1 < /dev/null
$
1 Like

Actually, this could work. awk uses the first parameter - after all option have been evaluated - as the program if nothing else ist supplied with e.g. the -f or -e option:

$ parm="NR>1 && \$2==\"c\""
$ echo $parm
NR>1 && $2=="c"
$ awk "$parm" file
3 c

Not sure though if it makes sense to supply variables like this...

1 Like

Thank you bipinajith - while that does work I think I may have oversimplified my problem. This awk statement is only one statement in a very complex script, and my parameters are not as simple as $2=c. In one case it may be ($2=='c' || $2==a) && $3=='y'... in another case it may be a completely different set of fields.

My question basically was can you pass full portions of if statment (including all syntax) to awk?? if not I will just have to change the script each time.

RudiC - that may work... thanks will play around with it and see where I get :smiley:

---------- Post updated at 01:46 AM ---------- Previous update was at 01:04 AM ----------

THANKS Rudi this works beautifully....

parm1=" && (\$2==\"c\" || \$2==\"a\")";
parm2="";
echo "$parm1";
echo "$parm2";
cat test | awk '{if ((1==1)'"$parm1""$parm2"') {print $0}}'

results:

 && ($2=="c" || $2=="a")
1       a
3       c

the reason I am keeping the cat is becuase I am doing various sorts and joins etc with the data prior to the awk statement.
The 1==1 means I don't have to check for the parm1 and parm2 variables being blank. even if they are blank the statement still works (will output all rows).