Thursday, 25 April 2013

Unix: Grep consecutive lines with duplicates field values

Here is a file :

8.txt  07:34:12 -> CONTENT START
8.txt  07:34:42 <-CONTENT END
8.txt  07:35:08 -> CONTENT START
8.txt  07:36:33 <-CONTENT END
8.txt  07:57:51 -> CONTENT START
8.txt  07:57:52 -> CONTENT START
8.txt  07:58:25 <-CONTENT END
8.txt  07:58:36 -> CONTENT START
8.txt  07:59:24 <-CONTENT END
8.txt  08:20:23 -> CONTENT START
8.txt  08:21:22 <-CONTENT END
8.txt  08:22:44 -> CONTENT START
8.txt  07:34:12 -> CONTENT START
8.txt  08:23:07 <-CONTENT END

Output expected:

8.txt 07:57:51 -> CONTENT START
8.txt 07:57:52 -> CONTENT START
8.txt 08:22:44 -> CONTENT START
8.txt 07:34:12 -> CONTENT START

That is we want only those lines which have same  value in the last field and are appearing consecutively.

It can be done as follows: 

$ uniq -D -f4 file

No comments:

Post a Comment