Search code examples
linuxbashawkfieldcut

linux cut command with field-value condition


I am trying to figure out a linux shell command which will display portion of a field from a log satisfying condition of other field.

Sample log-file:

2013-04-15;admin@abc.com;111111@abc.com;outgoing;24;headline
2013-04-15;222222@abc.com;admin@abc.com;incoming;0;chat
2013-04-15;admin@abc.com;333333@abc.com;outgoing;26;headline
2013-04-15;444444@abc.com;333333;incoming;12;chat
2013-04-16;444444@abc.com;555555;incoming;0;chat

I tried to display only the unique numbers from field 2 and 3 separated by delimiter ';' where field 5 != 0

Sample expected output:

111111
333333
444444

Solution

  • $ awk -F";" '$5!=0{print $2, $3}' fields.txt | grep -o '[0-9]\+' | sort -u
    111111
    333333
    444444
    

    Explanation: extract columns 2 and 3 based on column 5 value; match only the numbers; eliminate duplicates