Monday, July 18, 2016

Grep for first occurrence for multiple strings

Inspired by a post from stackexchange. I posted this as an answer there too.


 
$ cat somelog.log
ADWN    1259    11:00   B23
ADWN    3009    12:00   B19
DDWN     723    11:30   B04
ADWN    1589    14:20   B12
ADWN    1259    11:10   B23
DDWN    2534    13:00   B16
ADWN    3009    11:50   B14



Using "for" loop and "grep" we can do the following:

 
for i in $ (cut -d " " -f1 somelog.log | sort -u); do LC_ALL=C fgrep -m1 "$i" somelog.log; done 
 

Explanation:

$ cut -d " " -f1 somelog.log | sort -u # will result in unique identifiers
 
ADWN
DDWN
 
$ LC_ALL=C fgrep -m1 "$i" somelog.log # "m" option in grep will match the first pattern
 
1) for loop matches each pattern using grep and picks the first pattern. 
2) LC_ALL=C is used to make search faster 
3) fgrep is to match fixed strings instead of regular expressions. 

No comments:

Post a Comment