Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove line if field is duplicate

Tags:

sed

awk

Looking for an awk (or sed) one-liner to remove lines from the output if the first field is a duplicate.

An example for removing duplicate lines I've seen is:

awk 'a !~ $0; {a=$0}'

Tried using it for a basis with no luck (I thought changing the $0's to $1's would do the trick, but didn't seem to work).

like image 670
Kyle Avatar asked Apr 08 '10 23:04

Kyle


People also ask

How do you remove rows containing identical transactions?

Click on the data tab at the top of the screen Once you have selected the range, check the top of the screen and click the data tab. The different commands will be shown, and you should then check for 'remove duplicates' and click on it.

How do I remove duplicate rows in one column?

Remove Duplicates from a Single Column in Excel Select the data. Go to Data –> Data Tools –> Remove Duplicates. In the Remove Duplicates dialog box: If your data has headers, make sure the 'My data has headers' option is checked.

How do I remove duplicates but keep rows?

Remove duplicates but keep rest of row values with FilterWith a formula and the Filter function, you can quickly remove duplicates but keep rest. 5. Click Data > Filter to disable Filter, and remove the formulas as you need. You can see all duplicates have been removed and the rest of values are kept in the row.


1 Answers

awk '{ if (a[$1]++ == 0) print $0; }' "$@"

This is a standard (very simple) use for associative arrays.

like image 88
Jonathan Leffler Avatar answered Oct 21 '22 09:10

Jonathan Leffler