use unix commands to filter and sort data
Let's say I have data as开发者_JS百科 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.
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.
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
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
$
精彩评论