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
Linux 如何以字母顺序、递归方式对一组文件进行排序_Linux_Sorting_Find - Fatal编程技术网

Linux 如何以字母顺序、递归方式对一组文件进行排序

Linux 如何以字母顺序、递归方式对一组文件进行排序,linux,sorting,find,Linux,Sorting,Find,在Ubuntu上,我在一棵树中有一堆文件,它们都叫做“output.txt”。对于每个文件,我想用“sort”按字母顺序对行进行排序。我考虑 找到-name output.txt-exec sort{}\ 但是这会将排序后的行输出到控制台,而不是更新我想要的原始文件。我没有经常使用Linux,所以我可能弄错了,但是您不应该用StdOut来控制它吗 find . -name output.txt -exec sort{} \ > output.txt 我并没有经常使用Linux,所以我可能

在Ubuntu上,我在一棵树中有一堆文件,它们都叫做“output.txt”。对于每个文件,我想用“sort”按字母顺序对行进行排序。我考虑

找到-name output.txt-exec sort{}\


但是这会将排序后的行输出到控制台,而不是更新我想要的原始文件。

我没有经常使用Linux,所以我可能弄错了,但是您不应该用
StdOut
来控制它吗

find . -name output.txt -exec sort{} \ > output.txt

我并没有经常使用Linux,所以我可能弄错了,但您不应该用
StdOut
来控制它吗

find . -name output.txt -exec sort{} \ > output.txt
试试这个

csh <enter>

foreach i ( `find . -type f -name output.txt -print`) <enter>
   cat $i | sort > $.sorted <enter>
end <enter>
csh
foreach i(`find.-type f-name output.txt-print`)
类别$i |排序>$。已排序
结束
您将生成文件的排序副本。

试试这个

csh <enter>

foreach i ( `find . -type f -name output.txt -print`) <enter>
   cat $i | sort > $.sorted <enter>
end <enter>
find . -name output.txt -exec sort{} -o{} \;
csh
foreach i(`find.-type f-name output.txt-print`)
类别$i |排序>$。已排序
结束
您将生成文件的排序副本。

Fortran

find . -name output.txt -exec sort{} -o{} \;
我会做一些不同的事情,我不会把文件本身分类。以防排序后文件中添加任何新内容。下面是我要做的:

  • mirrior具有“output.txt”文件的目录树,并将排序后的文件放在那里
像这样:

#!/bin/bash 

LIST=`find $1 -name output.txt`

for i in $LIST ; do
        X=`dirname $i`
        mkdir -p "sorted/$X"
        sort "$i" -o "sorted/$i"
done
上面的内容需要稍加润色,mkdir应该有一个“如果”,并且应该检查$1是否正常

给你这个:

原文:

mytest
mytest/ccc
mytest/ccc/f
mytest/ccc/f/output.txt
mytest/ccc/d
mytest/ccc/d/output.txt
mytest/ccc/e
mytest/ccc/e/output.txt
mytest/bbb
mytest/bbb/f
mytest/bbb/d
mytest/bbb/e
mytest/aaa
mytest/aaa/f
mytest/aaa/f/output.txt
mytest/aaa/d
mytest/aaa/d/output.txt
mytest/aaa/e
mytest/aaa/e/output.txt
输出:

sorted
sorted/mytest
sorted/mytest/ccc
sorted/mytest/ccc/f
sorted/mytest/ccc/f/output.txt
sorted/mytest/ccc/d
sorted/mytest/ccc/d/output.txt
sorted/mytest/ccc/e
sorted/mytest/ccc/e/output.txt
sorted/mytest/aaa
sorted/mytest/aaa/f
sorted/mytest/aaa/f/output.txt
sorted/mytest/aaa/d
sorted/mytest/aaa/d/output.txt
sorted/mytest/aaa/e
sorted/mytest/aaa/e/output.txt
我认为最后一件事是将排序输出文件写入tmp文件,然后在完成排序时重命名,否则输出文件可能会在读取所有内容之前截断输入文件。就像sed的就地选项一样。

Fortran

我会做一些不同的事情,我不会把文件本身分类。以防排序后文件中添加任何新内容。下面是我要做的:

  • mirrior具有“output.txt”文件的目录树,并将排序后的文件放在那里
像这样:

#!/bin/bash 

LIST=`find $1 -name output.txt`

for i in $LIST ; do
        X=`dirname $i`
        mkdir -p "sorted/$X"
        sort "$i" -o "sorted/$i"
done
上面的内容需要稍加润色,mkdir应该有一个“如果”,并且应该检查$1是否正常

给你这个:

原文:

mytest
mytest/ccc
mytest/ccc/f
mytest/ccc/f/output.txt
mytest/ccc/d
mytest/ccc/d/output.txt
mytest/ccc/e
mytest/ccc/e/output.txt
mytest/bbb
mytest/bbb/f
mytest/bbb/d
mytest/bbb/e
mytest/aaa
mytest/aaa/f
mytest/aaa/f/output.txt
mytest/aaa/d
mytest/aaa/d/output.txt
mytest/aaa/e
mytest/aaa/e/output.txt
输出:

sorted
sorted/mytest
sorted/mytest/ccc
sorted/mytest/ccc/f
sorted/mytest/ccc/f/output.txt
sorted/mytest/ccc/d
sorted/mytest/ccc/d/output.txt
sorted/mytest/ccc/e
sorted/mytest/ccc/e/output.txt
sorted/mytest/aaa
sorted/mytest/aaa/f
sorted/mytest/aaa/f/output.txt
sorted/mytest/aaa/d
sorted/mytest/aaa/d/output.txt
sorted/mytest/aaa/e
sorted/mytest/aaa/e/output.txt

我认为最后一件事是将排序输出文件写入tmp文件,然后在完成排序时重命名,否则输出文件可能会在读取所有内容之前截断输入文件。类似于sed的就地选项。

上面的命令将在单个文件中生成所有已排序的行,而不是对每个文件进行排序。“sort-o”在这里是否有帮助?上面的命令将在单个文件中生成所有已排序的行,而不是对每个文件进行排序。“sort-o”在这里是否有帮助?现在是正确的。以前的版本只生成一个文件,所有行都在一起。对不起,现在是正确的。以前的版本只生成一个文件,所有行都在一起。抱歉。看起来它正在工作(需要一段时间,所以不能确定)。我可以发誓我试过类似的方法(不是一直都是这样吗?)不管怎样,你都很震撼。你真的能用sort来使用和inputfile相同的outputfile吗?我怀疑它。@lothar不管你怀疑与否,它仍然可以工作:-p排序程序读取整个文件,对内存中的行进行排序,然后写入输出看起来它正在工作(需要一段时间,所以不能确定)。我可以发誓我试过类似的方法(不是一直都是这样吗?)不管怎样,你都很震撼。你真的能用sort来使用和inputfile相同的outputfile吗?我怀疑它。@lothar不管你怀疑与否,它仍然可以工作:-p排序程序读取整个文件,对内存中的行进行排序,然后写入输出