Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply if function to a field inside another field?

I have a table that I want to filter by awk. This is an example of how I want to do it:

Berlin  BG  AD=14;CD=0.05 
Cairo   CE  AD=9;CD=0.01 
Toronto TC  AD=23;CD=0.17
Sydney  SA  AD=2;CD=0.11 
Tokyo   TJ  AD=19;CD=0.22

I want to filter the fields based on the AD value and output all fields if in that line AD is equal to or greater than 10.

The result should be like that:

Berlin  BG  AD=14;CD=0.05
Toronto TC  AD=23;CD=0.17
Tokyo   TJ  AD=19;CD=0.22

I tried this script:

awk '{if (awk '{print $3}' temp.txt | awk -F";" '{print $1}' | awk -F"=" '{print $2}' >= 10) print $0}' temp.txt

But it gave me such a syntax error about an unexpected newline or end of the string

like image 568
Dr. Heinz Doofenshmirtz PhD Avatar asked Sep 02 '25 10:09

Dr. Heinz Doofenshmirtz PhD


1 Answers

With your shown samples and in GNU awk you could try following awk code. Using match function of awk where mentioning regex [[:space:]]+(AD=)([0-9]+) which creates 2 capturing group and stores matched values into array named arr. Then checking condition if 2nd value is greater than 10 then print that line.

awk '
match($0,/[[:space:]]+(AD=)([0-9]+)/,arr) && arr[2]>10
'  Input_file
like image 108
RavinderSingh13 Avatar answered Sep 04 '25 00:09

RavinderSingh13