TECH SOLUTIONS

Click here to edit subtitle

Forums

Post Reply
Forum Home > Unix Learnings > Grep consecutive lines with duplicates field values

Sourav Gulati
Site Owner
Posts: 83

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

The option -D is for displaying the duplicated lines and -f4 skips the first four fields.




--


January 7, 2013 at 1:39 AM Flag Quote & Reply

You must login to post.