Hi Moderators,
My sincere apologies. @nezabudka, Please see the below code
#Shell arguments
#-------------------
sh samplecollector.sh "20200212" "1=CT,2=US_10,3=CT_US_10"
#Inside the script
#----------------------
FILEDATE=$1
CONDITIONS=$2 # Used the values directly in the variable in my post, the condition can be one or many based on user input
/usr/bin/awk -v COND=${CONDITIONS} -F"," 'function strcondition(h, i,F,v,b,dec) {F=split(h,v,",");for(i=1;i<=F;i++) {split(v,b,"=");dec=(dec != "") ? dec""sprintf(" %s == \"%s\" &&",lookup(b[1]),b[2]) : sprintf("%s == \"%s\" &&",lookup(b[1]),b[2]);} sub(/[ &&\t\r\n]+$/, "", dec);return dec;} function lookup(l, f,ar){ar[1]="$1";ar[2]="$4";ar[3]="$60";ar[4]="$17";ar[5]="$4";if (l in ar) {return ar[l];}} { if(strcondition(COND));{print COND"|"$147"|"$15"|"$16"|"$17"|"$4}}' Record_${FILEDATE}.dat
The if (...); will do nothing.
The print CONDITION will print an empty entity as the variable is not defined.
Your idea / approach will not work per se. You will need to compile your COND in, e.g. have some text tool write a script which then will be executed by awk .
Before I post i store the condition in a variable as like below(in 2 steps) and after that i merged in the same awk as per above snippet, however the problem statement remains the same where iam unable to execute the if statement with dynamically formed condition.
Thats cool rdrtx1. However anxious to see how OR condition works as below are AND conditions.
In our example the below conditions are treated as AND conditions and file got processed.
CONDITIONS="1=CT,2=US_10,3=CT_US_10"
converted to
CONDITIONS="1=CT,4=US_10,60=CT_US_10"
Can you please throw some light on how to work with AND,OR conditions together if the condition comes dynamically from user input. Below is an example.
CONDITIONS="(1=CT or 2=US_10) and 3=CT_US_10 and 4="