Last active
January 30, 2016 11:59
-
-
Save sudar/5201701 to your computer and use it in GitHub Desktop.
Awk command to remove duplicate lines, based on a field. Explanation at http://sudarmuthu.com/blog/remove-duplicate-lines-based-on-a-field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Before starting | |
x = {} | |
# After line 1 | |
x = { | |
CTO => 1 | |
} | |
# After line 2 | |
x = { | |
CTO => 1 | |
Manager => 1 | |
} | |
# After line 3 | |
x = { | |
CTO => 1 | |
Manager => 1 | |
CEO => 1 | |
} | |
# After line 4 | |
x = { | |
CTO => 1 | |
Manager => 2 | |
CEO => 1 | |
} | |
# After line 5 | |
x = { | |
CTO => 1 | |
Manager => 2 | |
CEO => 1 | |
CFO => 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
awk '!x[$2]++' filename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
if (x[$2] == 0 ) | |
x[$2]++ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tom CTO 32 | |
Harry Manager 45 -> Manager field is duplicate | |
Krish CEO 50 | |
Bob Manager 49 -> Manager field is duplicate | |
Patrick CFO 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tom CTO 32 | |
Harry Manager 45 | |
Krish CEO 50 | |
Patrick CFO 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment