bash: Unique lines based on specific fields.
I was wondering if anyone knows a simpler way to sort the lines in a file based on particular (non-adjacent) fields. Consider this sample file:
Code:
three apple 1 Code:
$ awk '{print $1,$3,$2}' temp.txt | sort -k 1,2 -u | awk '{print $1,$3,$2}' Any thoughts? |
Code:
$ sort -k 1 -k 3 -u myfile Otherwise, try Code:
$ sort -k 1 -u | sort -k 3 -u |
Quote:
Code:
$ sort -k 1 -k 3 -u temp.txt The output in the second case eliminated "one orange 5", which was unique. I kept scratching my head because I thought the first soulution ought to work. Then I tried: Code:
sort -k 1,1 -k 3 -u temp.txt Thanks, I knew there had to be an easier way! EDIT: I accidently posted the wrong output in the final solution. Corrected in my next post. |
Quote:
Congrats on getting it solved. --Jonas |
Quote:
Code:
one lime 3 |
Quote:
The actual output I get is: Code:
$ sort -k 1,1 -k 3 -u temp.txt |
Quote:
BTW, it's not in the man page (perhaps in the <shudder /> info page), but you can fine tune your keys to the character position: Code:
sort -k f.n,g.m |
All times are GMT -5. The time now is 04:23 AM. |