0

我有一个文件,内容如下:

onelab2.warsaw.rd.tp.pl    5
onelab3.warsaw.rd.tp.pl    5
lefthand.eecs.harvard.edu  7
righthand.eecs.harvard.edu 7
planetlab2.netlab.uky.edu  8
planet1.scs.cs.nyu.edu     9
planetx.scs.cs.nyu.edu     9

所以对于每一行,有一个数字我想要每个数字的第一行,所以对于上面的内容,我想得到:

onelab2.warsaw.rd.tp.pl    5
lefthand.eecs.harvard.edu  7
planetlab2.netlab.uky.edu  8
planet1.scs.cs.nyu.edu     9

我怎样才能做到这一点?我希望使用 awk、sed 等的 shell 脚本。

4

4 回答 4

7

这可能对您有用(GNU 排序):

sort -nsuk2 file

-k2以数字方式对第二个字段进行排序,-n保持-s原始顺序并-u删除重复项。

于 2013-04-22T05:53:50.697 回答
1

为此使用awk命令:

awk '{if(!a[$2]){a[$2]=1; print}}' file.dat

解释:

{
  # 'a' is a lookup table (array) which will contain all numbers
  # that have been printed so far. It will be initialized as an empty
  # array on its first usage by awk. So you don't have to care about.
  # $2 is the second 'column' in the line -> the number
  if(!a[$2]) 
  {
    # set index in the lookup table. This way the if statement will 
    # fail for the next line with the same number at the end
    a[$2]=1;
    # print the whole current line
    print
  }
}
于 2013-04-21T23:44:24.077 回答
0

使用 sort 和 uniq:

sort -n -k2 input | uniq -f1
于 2013-04-22T06:10:38.687 回答
0
perl -ane 'print unless $a{$F[1]}++' file
于 2013-04-22T07:03:09.807 回答