Xargs 的最佳输出格式

发布于 2024-08-19 22:17:33 字数 485 浏览 1 评论 0原文

我正在编写一个简单的程序来运行系统上各个目录中的一堆文件。它基本上涉及打开它们并检查有效的 XML。该程序的选项之一是列出错误的 xml 文件。

这引出了我的问题。将其格式化以与 XARGS 一起使用的最佳输出是什么?我认为将每个条目放在换行符上就足够了,但这似乎有点令人困惑。因为文件名都有空格。

所以说我的输出是:

./dir name 1/file 1.xml
./dir name 2/file 2.xml
./dir name 3/file 3.xml

我尝试了以下命令,但它一直说“没有这样的文件或目录”。

./myprogram.py --list BADXML | xargs -d '\n' cat

所以..我要么误解了如何使用 XARGS,要么我需要稍微改变程序输出的格式。我不确定来这里的最佳最容易使用的路线。如果我可以避免的话,我不想总是输入一堆 xarg 选项。

I'm writing a simple program to run through a bunch of files in various directories on my system. It basically involves opening them up and checking for valid XML. One of the options of this program is to list bad xml files.

This leads me to my question. What the best output to format this for use with XARGS. I thought putting each entry on a newline would be good enough, but it seems a bit confusing. because the filenames all have spaces.

So say my output is:

./dir name 1/file 1.xml
./dir name 2/file 2.xml
./dir name 3/file 3.xml

I tried the following command, but it keeps saying "No such file or directory".

./myprogram.py --list BADXML | xargs -d '\n' cat

So.. I am either misunderstanding how to use XARGS or I need to slightly change the format of the output of my program. I am not sure the best easiest to use) route to take here. i would hate to have to always type a mess of xarg options if I can avoid it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缪败 2024-08-26 22:17:33

man xargs

--空

-0 输入项以空字符而不是
空格、引号和
反斜杠并不特殊(每个
字符按字面意思理解)。
禁用文件结尾字符串,这
与任何其他参数一样对待。
当输入项可能包含时很有用
空格、引号或
反斜杠。 GNU find -print0
选项产生适合的输入
这种模式。

man xargs

--null

-0 Input items are terminated by a null character instead of by
whitespace, and the quotes and
backslash are not special (every
character is taken literally).
Disables the end of file string, which
is treated like any other argument.
Useful when input items might contain
white space, quote marks, or
backslashes. The GNU find -print0
option produces input suitable for
this mode.

不奢求什么 2024-08-26 22:17:33

您可以放弃 xargs,并使用 read:

./myprogram.py --list BADXML | while read -a line; do cat "${line[*]}"; done

xargs 可以做的任何事情,while-read 循环可以做得更好...

Postscript 根据我的 什么时候 xargs 应该优先于 while-read-loops 问题,答案强调了 xargs 的非常强大的效率案例,尽管它使用一些额外的脚本来模拟 xargs 的参数聚集并不太困难,例如

batch10cat () {
    local i=1 argv line
    declare -a argv
    while read -r line; do
        argv[i]="$line"
        let i++
        if test $i -gt 10; then i=1; cat "${argv[@]}"; fi
    done
    if test $i -gt 1; then cat "${argv[@]}"; fi
}
./myprogram.py --list BADXML | batch10 cat

You could ditch xargs, and use read:

./myprogram.py --list BADXML | while read -a line; do cat "${line[*]}"; done

Anything xargs can do, while-read loops can do better...

Postscript Per my When should xargs be preferred over while-read-loops question, the answers stressed a very strong efficiency case for xargs, although it is not too difficult to simulate the argument bunching of xargs with some extra scripting, e.g.

batch10cat () {
    local i=1 argv line
    declare -a argv
    while read -r line; do
        argv[i]="$line"
        let i++
        if test $i -gt 10; then i=1; cat "${argv[@]}"; fi
    done
    if test $i -gt 1; then cat "${argv[@]}"; fi
}
./myprogram.py --list BADXML | batch10 cat
护你周全 2024-08-26 22:17:33

使用 GNU Parallel http://www.gnu.org/software/parallel/ 你应该能够在不更改 myprogram.py 的情况下完成此操作:

./myprogram.py --list BADXML | parallel cat

额外的好处:猫将并行运行,因此在多核计算机上可能会更快。

With GNU Parallel http://www.gnu.org/software/parallel/ you should be able to do it with no change to myprogram.py:

./myprogram.py --list BADXML | parallel cat

Added bonus: the cat will run in parallel and may thus be faster on multicore computers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文