Skip to content

Instantly share code, notes, and snippets.

@leikind
Created Jun 18, 2010
Embed
What would you like to do?
$ cat in.txt
1000,1030
1000,1040
1040,1000
3000,3010
3010,3000
# with ruby
cat in.txt | ruby -n -e 'BEGIN{@a = {}}; x,y = $_.chomp.split(","); if y && x && ! y.empty? then @a[x] = y; puts $_ ; end ; END{@a.keys.each{|k| puts k + "," + k}}'
# with awk
for e in `awk 'BEGIN{RS=","}{print $1}' < in.txt | sort | uniq ` ; do echo $e,$e; done && cat in.txt | sort -u
# with cut
(for e in `cut -d, -f1 < in.txt ` ; do echo $e,$e; done && cat in.txt) | sort -u
# Champion: cut + xargs
(cut -d, -f1 < in.txt | xargs -n1 -I{} echo {},{}) && cat in.txt | sort -u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment