linux cut command with field-value condition

7.8k views Asked by At

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;[email protected];[email protected];outgoing;24;headline
2013-04-15;[email protected];[email protected];incoming;0;chat
2013-04-15;[email protected];[email protected];outgoing;26;headline
2013-04-15;[email protected];333333;incoming;12;chat
2013-04-16;[email protected];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
2

There are 2 answers

1
karakfa On BEST ANSWER
$ 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

1
adelphus On
grep -Po '\d+(?=([^;]*;)?[^;]*;[^;]+;[^0][^;]*;[a-z]+$)' logfile|sort -u

Should be self-explanatory. lol.