linux 中实现数据的每一行进行排序

发布时间 2023-04-21 21:16:54作者: 小鲨鱼2018

 

001、

(base) [b20223040323@admin1 test2]$ ls
a.txt
(base) [b20223040323@admin1 test2]$ cat a.txt    ## 测试数目
6 8 9 3 7
5 2 5 6 8
8 2 4 2 7
(base) [b20223040323@admin1 test2]$ for i in {1..3}; do sed -n "$i"p a.txt | sed 's/ /\n/g' | sort | paste -s -d " " >> b.txt; done
(base) [b20223040323@admin1 test2]$ ls                      ## 每一行正向排序
a.txt  b.txt
(base) [b20223040323@admin1 test2]$ cat b.txt       ## 排序结果
3 6 7 8 9
2 5 5 6 8
2 2 4 7 8
(base) [b20223040323@admin1 test2]$ for i in {1..3}; do sed -n "$i"p a.txt | sed 's/ /\n/g' | sort -r | paste -s -d " " >> c.txt; done
(base) [b20223040323@admin1 test2]$ ls
a.txt  b.txt  c.txt
(base) [b20223040323@admin1 test2]$ cat c.txt      ## 每一行实现逆向排序
9 8 7 6 3
8 6 5 5 2
8 7 4 2 2