Hi,
i have a scenario were i should compare a few fields from each line then increment a variable based on that.
Example file
989878|8999|Y|0|Y|N|V
989878|8999|Y|0|N|N|V
989878|8999|Y|2344|Y|N|V
i have 3 conditions to check and increment a variable on every line
condition 1 if ( $3 =='Y' && $4==0 && $6=N)
then variable1++
cond2 if ( $3 =='Y' && $6=N)
then varable2++
Desired output:
count of variable1 and count of variable2 and
My script is pretty big which extracts data from db into a unl file then am doing this condition check and few other, only thing am not comfortable is what i have mentioned please help
awk -F '|' ' $3 =="Y" && $4=="0" && $6=="N" {var1++}
$3 =="Y" && $6=="N" {var2++}
{next}
END{print "condition 1", var1, " condition2", var2}' inputfile
Note for every cond1==true : cond2 is also true, if you meant for them to be mutually exclusive the above does not work. This will:
awk -F '|' ' $3 =="Y" && $4=="0" && $6=="N" {var1++; next}
$3 =="Y" && $6=="N" {var2++}
{next}
END{print "condition 1", var1, " condition2", var2}' inputfile
1 Like
hey Jimm
thanks for looking at the post and repsponding
i think i confused you with cond 1 and cond2 , the thing is i have 2-3 conditions and in all instances i will have to get the count of how many times it has occurred, but i tried it prints only one value var1 and not var2 or var3
awk -F '|' ' $3 =="Y" && $4=="0" && $6=="N" {var1++; next}
$3 =="Y" && $6=="N" {var2++}
{next}
END{print "condition 1", var1, " condition2", var2}' inputfil
The exclusive stuff with next in
awk -F '|' '
$3 =="Y" && $4=="0" && $6=="N" {var1++}
$3 =="Y" && $6=="N" {var2++}
END {
print "condition1", var1+0
print "condition2", var2+0
}
' inputfile
1 Like
Like this?
awk -F"|" '{
if ($3=="Y" && $4==0 && $6=="N")
variable1++
if ($3 =="Y" && $6=="N")
variable2++
}
END {print "variable1=" variable1 "\nvariable2=" variable2}' inputfil
1 Like