Ind*_*ial 118 shell find xargs sort
我需要能够在将其输出find到命令之前按字母顺序对输出进行排序。进入| sort |之间不起作用,那我该怎么办?
find folder1 folder2 -name "*.txt" -print0 | xargs -0 myCommand
Run Code Online (Sandbox Code Playgroud)
Oli*_*Oli 85
find像往常一样使用并用 NUL 分隔你的行。GNUsort可以使用 -z 开关处理这些:
find . -print0 | sort -z | xargs -r0 yourcommand
Run Code Online (Sandbox Code Playgroud)
Arc*_*ege 66
的某些版本sort有一个-z选项,它允许以空字符结尾的记录。
find folder1 folder2 -name "*.txt" -print0 | sort -z | xargs -r0 myCommand
Run Code Online (Sandbox Code Playgroud)
此外,您还可以编写一个高级脚本来执行此操作:
find folder1 folder2 -name "*.txt" -print0 | python -c 'import sys; sys.stdout.write("\0".join(sorted(sys.stdin.read().split("\0"))))' | xargs -r0 myCommand
Run Code Online (Sandbox Code Playgroud)
添加-r选项以xargs确保myCommand使用参数调用。
我认为你需要-n排序标志#
根据人排序:
-n, --numeric-sort
compare according to string numerical value
Run Code Online (Sandbox Code Playgroud)
编辑
print0 可能与此有关,我刚刚对此进行了测试。取出print0,您可以使用-z标志空终止排序中的字符串
如果你安装了 GNU Parallel http://www.gnu.org/software/parallel/你可以这样做:
find folder1 folder2 -name "*.txt" -print |
sort |
parallel myCommand
Run Code Online (Sandbox Code Playgroud)
您可以简单地通过以下方式安装 GNU Parallel:
wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem
Run Code Online (Sandbox Code Playgroud)
观看 GNU Parallel 的介绍视频以了解更多信息:https : //www.youtube.com/playlist?list=PL284C9FF2488BC6D1
| 归档时间: |
|
| 查看次数: |
164981 次 |
| 最近记录: |