如何使用命令从左到右按升序排列vi
?sort
排列前的示例:
2 3
3 6
5 1
4 2
5 8
我想要的是这样的:
2 3
3 6
1 5
2 4
5 8
答案1
这可以是以下命令:
while read p;do echo $p|tr ' ' '\n'|sort -k1|paste -s -d' ' -;done<input>output
用实际的 IO 文件替换输入和输出。
我写了一个小脚本,也可以用来测试这一点:
#!/bin/bash
# This script is just for testing purposes.
# Call this like:
# sort_numbers numbers.txt numbers_correct.txt
# Where numbers.txt is the file to sort and
# numbers_correct.txt is a file where they are
# sorted correct.
while read p; do
echo $p|tr ' ' '\n'|sort -k1|paste -s -d' ' -
done<$1>output.$1
# This just compares the output with the correct output
file1=$2
file2=output.$1
echo -n $file1 and $file2 are' ' && cmp --silent $file1 $file2 && echo the same || echo different