If condition on awk

Hi All,

Would you guys help me?

I have a file that consists of several unstructured fields. in this file I will take the code field and count_berry field.
but the position of the count_berry field is always changing.the column for code is always structured, which is found in column 6
I have found the count_berry field to be in a definite column, that is in columns 10, 12 (count_berry, 100) then in column 14.16 (count_berry, 10) then in column 24.26 (count_berry, 200).
so I can create a script to retrieve the columns and values by using awk like this:

cat file |awk '$10 == "\"count_berry\"" {print $6 "," $12 };$14 == "\"count_berry\"" {print $6 "," $16};$24 == "\"count_berry\"" {print $6 "," $26}' 

the problem is, how do I get an unknown count_berry column? can anyone help me make the if condition code on awk?

Try so

awk '{ for ( i=1; i <= NF; i++ ) if ( $i ~ var ) print $6,$i }' var=count_berry file
1 Like

In this case it is $(i+2)

awk '{ for ( i=1; i <= NF; i++ ) if ( $i==var ) print $6,$(i+2) }' var='"count_berry"' file
2 Likes

Thankyou so much !!

How about

awk '{match ($0, /count_berry/); split (substr ($0, RSTART), T); print $6, T[3]}' file
2 Likes