Jay
Jay

Reputation: 23

Unix - removing duplicate headers from file


I have a file (result.txt) which is having few duplicate header lines, I want to retain only one header line and remove all other duplicates (refer desired output). I tried using sort -u, uniq, sed and all other options but I couldn't able to get the desired output. Could someone help me on this.

cat result.txt
Metric              date_sk  date_sk -7 
----------------  ----------  ---------- 
Test1            2015-10-19  2015-10-12
Metric                 date_sk  date_sk -7 
----------------  ----------  ---------- 
Test2            2015-10-19  2015-10-12  
Metric               date_sk  date_sk -7 
----------------  ----------  ---------- 
Test3            2015-10-19  2015-10-12

Desired output:
Metric              date_sk  date_sk -7 
----------------  ----------  ---------- 
Test1            2015-10-19  2015-10-12
Test2            2015-10-19  2015-10-12 
Test3            2015-10-19  2015-10-12

Thanks

Upvotes: 0

Views: 612

Answers (1)

Cyrus
Cyrus

Reputation: 88686

Try this with GNU sed:

sed '3,${/^Metric/d;/^---/d}' file

Output:

Metric              date_sk  date_sk -7 
----------------  ----------  ---------- 
Test1            2015-10-19  2015-10-12
Test2            2015-10-19  2015-10-12  
Test3            2015-10-19  2015-10-12

If you want to edit "in place" add sed's option -i.

Upvotes: 1

Related Questions