Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 仅对CVS表中的单个列进行排序_Sorting_Awk - Fatal编程技术网

Sorting 仅对CVS表中的单个列进行排序

Sorting 仅对CVS表中的单个列进行排序,sorting,awk,Sorting,Awk,是否有一种单步方法使用awk、sort或等同于对多列CSV表中的一列进行排序或反转,同时保持其余列的顺序不变 例如,我有: 6, 45, 9 5, 47, 6 4, 46, 7 3, 48, 4 2, 10, 5 1, 11, 1 并希望: 1, 45, 9 2, 47, 6 3, 46, 7 4, 48, 4 5, 10, 5 6, 11, 1 因此,只对第一列进行排序,其余列按其先前的顺序进行排序。这可能适用于您: paste -d, <(cut -d, -f1 file |

是否有一种单步方法使用awk、sort或等同于对多列CSV表中的一列进行排序或反转,同时保持其余列的顺序不变

例如,我有:

6, 45, 9
5, 47, 6
4, 46, 7
3, 48, 4
2, 10, 5
1, 11, 1
并希望:

1, 45, 9
2, 47, 6
3, 46, 7
4, 48, 4
5, 10, 5
6, 11, 1  

因此,只对第一列进行排序,其余列按其先前的顺序进行排序。

这可能适用于您:

paste -d, <(cut -d, -f1 file | sort) <(cut -d, -f2- file)
paste-d,awk一行

awk -F, '{c[NR]=$1;l[NR]=$2", "$3}END{for(i=1;i<=NR;i++) print c[NR-i+1]", "l[i]}' file

awk-F,'{c[NR]=$1;l[NR]=$2“,“$3}END{for(i=1;i如果你有
GNU awk
这里有一行代码:

$ gawk '{s[NR]=$1;c[NR]=$2 $3}END{for(i=0;++i<=asort(s);)print s[i] c[i]}' file
1,45,9
2,47,6
3,46,7
4,48,4
5,10,5
6,11,1
将其保存到文件
sort.awk
并执行
awk-f sort.awk文件

$ awk -f sort.awk file
1,45,9
2,47,6
3,46,7
4,48,4
5,10,5
6,11,1

应该注意的是,这不会对第一列进行排序,它只是反向打印,不会处理未排序的数据。@sudo_O是的,你是对的。我在OP的问题中看到了“排序或等于对单个列进行排序或反转”。@Kent darn,我刚刚发布了同样的内容,然后看到了你的。+1并删除了我的内容。
{ # read col1 in sort array, read others in col array
  sort[NR] = $1
  cols[NR] = $2 $3
}
END { # sort it with bubble sort
    do {
    haschanged = 0
    for(i=1; i < NR; i++) {
      if ( sort[i] > sort[i+1] ) {
    t = sort[i]
    sort[i] = sort[i+1]
    sort[i+1] = t
    haschanged = 1
      }
    }
  } while ( haschanged == 1 )

  # print it
  for(i=1; i <= NR; i++) {
      print sort[i] cols[i]
  }
}
$ awk -f sort.awk file
1,45,9
2,47,6
3,46,7
4,48,4
5,10,5
6,11,1