对值进行排序并输出其已排序列的索引

时间:2014-02-26 22:39:56

标签: sorting unix awk cut

我有一个看起来像的文件:

20 30 40
80 70 60
50 30 40

每列代表一个程序。我想知道程序对每一行的作用。我的理想输出是

3 2 1
1 2 3
1 3 2

即。在第1行中,第三列具有最高值,接着是第二列,然后是第一列(这可以颠倒,无所谓)。

我该怎么做?

5 个答案:

答案 0 :(得分:2)

我会使用其他一些Unix工具(readcatsortcuttrsed执行此操作,当然还有bash

while read line
do
  cat -n <(echo "$line" | sed 's/ /\n/g') | sort -r -k +2 | cut -f1 | tr '\n' ' '
  echo
done < input.txt

输出如下:

 3      2      1 
 1      2      3 
 1      3      2 

答案 1 :(得分:1)

使用Python的另一种解决方案:

$ python
Python 2.7.6 (default, Jan 26 2014, 17:25:18)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> with open('file.txt') as f:
...    lis=[x.split() for x in f]
...
>>> for each in lis:
...     each = [i[0] + 1 for i in sorted(enumerate(each), key=lambda x:x[1], reverse=True)]
...     print ' '.join([str(item) for item in each])
...
3 2 1
1 2 3
1 3 2

答案 2 :(得分:1)

使用Gnu Awk第4版:

$ awk 'BEGIN{ PROCINFO["sorted_in"]="@val_num_desc" }
{
    split($0,a," ")
    for (i in a) printf "%s%s", i,OFS
    print ""
}' file

3 2 1 
1 2 3 
1 3 2 

答案 3 :(得分:0)

如果您有GNU awk,那么您可以执行以下操作:

awk '{
    y = a = x = j = i = 0;
    delete tmp;
    delete num;
    delete ind;
    for(i = 1; i <= NF; i++) {
        num[$i, i] = i
    }
    x = asorti(num)
    for(y = 1; y <= x; y++) {
        split(num[y], tmp, SUBSEP)
        ind[++j] = tmp[2]
}
for(a = x; a >= 1; a--) {
    printf "%s%s", ind[a],(a==1?"\n":" ")
}
}' file

$ cat file
20 30 40
0.923913 0.913043 0.880435 0.858696 0.826087 0.902174 0.836957 0.880435
80 70 60
50 30 40

awk '{
    y = a = x = j = i = 0;
    delete tmp;
    delete num;
    delete ind;
    for(i = 1; i <= NF; i++) {
        num[$i, i] = i
    }
    x = asorti(num)
    for(y = 1; y <= x; y++) {
        split(num[y], tmp, SUBSEP)
        ind[++j] = tmp[2]
}
for(a = x; a >= 1; a--) {
    printf "%s%s", ind[a],(a==1?"\n":" ")
}
}' file
3 2 1
1 2 6 8 3 4 7 5
1 2 3
1 3 2

答案 4 :(得分:0)

通过perl解决方案

#!/usr/bin/perl
open(FH,'<','/home/chidori/input.txt') or die "Can't open file$!\n";
while(my $line=<FH>){
        chomp($line);
        my @unsorted_array=split(/\s/,$line);
        my $count=scalar @unsorted_array;
        my @sorted_array = sort { $a <=> $b } @unsorted_array;
        my %hash=map{$_ => $count--} @sorted_array;

        foreach my $value(@unsorted_array){
                print "$hash{$value} ";
        }

        print "\n";
}