这里需要一些帮助。
我有两份文件,
file1.txt >
5555555555
1111111111
7777777777
file2.txt >
0000000000
8888888888
2222222222
4444444444
3333333333当我运行时,$ sort -m file1.txt file2.txt > file-c.txt输出文件-c.txt在file1和file2中合并,但它没有排序。
file-c.txt >
0000000000
5555555555
1111111111
7777777777
8888888888
2222222222
4444444444
3333333333当它发生的时候,我需要一个错误,告诉我文件(file1和file2)没有排序,合并在排序之前无法合并文件。因此,当我运行$ sort -m file1.txt file2.txt > file-c.txt时,我必须得到一个错误,指出它不能将file1和file2合并到文件-c中,因为它们还没有排序。
希望你们能理解我:D
发布于 2013-08-07 21:31:18
如果我明白你在问什么,你可以这样做:
DIFF1=$(diff <(cat file1.txt) <(sort file1.txt))
DIFF2=$(diff <(cat file2.txt) <(sort file2.txt))
if [ "$DIFF1" != "" ]; then
echo 'file1 is not sorted'
elif [ "$DIFF2" != "" ]; then
echo 'file2 is not sorted'
else
sort -m file1.txt file2.txt
fi这在Bash (和其他shell)中起作用,并执行以下操作:
的命令行排序
这是你要找的东西吗?
编辑:交替使用每个@twalberg如果你的sort版本支持它,你可以这样做:
if ! sort -c file1.txt
then echo 'file1 is not sorted'
elif ! sort -c file2.txt
then echo 'file2 is not sorted'
else
sort -m file1.txt file2.txt
fihttps://stackoverflow.com/questions/18104381
复制相似问题