text-processing awk – How to Filter Based on Field Value in awk

awktext processing

I want to print the countries which which have to after ~:.

welcome~:to~:Germany
welcome~:no please~:Italy
welcome~:to~:Brazil
welcome~:not ok~:China

Note: I do not know what will be there other than "to". It can be anything and can be changed (like no please, not ok).

I am tried using cut, awk, sed. But I am unable to figure out.

awk -F "~:" '{print $2 $NF}'

But I get output like:

toGermany
no pleaseItaly
toBrazil
not okChina

How to filter countries other than to.

UPDATE:

Alternative solution:(Figured out with cut) 🙂

grep -v "to" |cut -d ':' -f3 

Best Answer

As you are using ~: as field separator you can check if second field is equal to to and to print third field which is the country:

awk -F"~:" '$2 == "to" { print $3; }' file

Result:

Germany
Brazil
Related Question