1

Let's say I have data as below:

aaa m7
aaa m9
aaa m7
aaa m7
aaa m7
ccc m9
ccc m7
ccc m7
bbb m7
bbb m9
bbb m7
ddd m7

How could I sort it into:

aaa m9
bbb m9
ccc m9
ddd m7

using unix commands (sort, uniq, etc.)?

What I need is sort all data and then filter out all data with same field#1.

8
  • 2
    That looks filtered, not sorted. Commented Nov 12, 2010 at 6:05
  • edit my question, hope it is clear now Commented Nov 12, 2010 at 6:07
  • 2
    What is it that you want to do? Commented Nov 12, 2010 at 6:09
  • Your edit hasn't really changed anything. Commented Nov 12, 2010 at 6:09
  • it is combination of sort and filter Commented Nov 12, 2010 at 6:12

3 Answers 3

3

This takes your input and gives your output. I'm not sure whether it's what you want, given the vagueness of your specification...

$ cat ./4162059.awk 
#!/usr/bin/awk -f

{
    if (d[$1] < $2) {
        d[$1] = $2
    }
}

END {
    for (k in d) {
       print k " " d[k]
    }
}

$ cat ./4162059.txt 
aaa m7
aaa m9
aaa m7
aaa m7
aaa m7
ccc m9
ccc m7
ccc m7
bbb m7
bbb m9
bbb m7
ddd m7

$ ./4162059.awk 4162059.txt | sort
aaa m9
bbb m9
ccc m9
ddd m7

The awk program keeps note of the value of the column 1 with the 'highest' value of column 2 and prints them once it has parsed the whole input file. The output is then sorted by sort on the command-line.

Sign up to request clarification or add additional context in comments.

2 Comments

Awk is under-used and great for this kind of thing. +1.
Thanks @Conrad. It's underused by me, too. I tend to use it only as a cut + grep hybrid :-)
0

If the data resides in data.txt, use:

sort < data.txt > sorted.txt

If you just want the m9's, use:

grep m9 < data.txt | sort | uniq > sorted_m9.txt

Comments

0

It's not very clear what you want to do.

I'm assuming you want to print only unique values in column 1 with the largest corresponding value in column 2 because your output has m9.

sort -V input | 
perl -nae '$H{$F[0]}=$F[1];END{for(sort keys %H){print $_." ".$H{$_}."\n";}}'

Sample run:

$ cat file
aaa m9
aaa m10
ccc m9
ccc m7
bbb m7
bbb m9
bbb m7
ddd m11
ddd m1
$ sort -V file | perl -nae '$H{$F[0]} = $F[1];END{for(sort keys %H){print $_." ".$H{$_}."\n";}}'
aaa m10
bbb m9
ccc m9
ddd m11
$ 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.