AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 516602
Accepted
gordo911
gordo911
Asked: 2013-06-19 03:39:32 +0800 CST2013-06-19 03:39:32 +0800 CST 2013-06-19 03:39:32 +0800 CST

如何使用 xargs 处理以空格分隔的环境变量

  • 772

我正在尝试并行多次执行长时间运行的过程。每次执行进程的参数都存储在一个以空格分隔的环境变量中。这是我要执行的人为示例:

$ echo -e 1 2 3 4 | xargs --max-procs=3 --max-args=1 --replace=% echo % is the number being processed

这是该命令的输出:

1 2 3 4 is the number being processed

为什么 max-args 似乎被忽略了?然后我尝试显式设置分隔符以提供更好的结果:

$ echo -e 1 2 3 4 | xargs -d " " --max-procs=3 --max-args=1 --replace=% echo % is the number being processed
1 is the number being processed
2 is the number being processed
3 is the number being processed
4
 is the number being processed

xargs 在处理第 4 个参数时在做什么?

经过一番搜索,我确实设法几乎得到了我想要的东西。参数被正确处理,但并行性不起作用(使用此处未显示的另一个命令验证):

$ echo -e 1 2 3 4 | xargs -n 1 | xargs --max-procs=3 --max-args=1 --replace=% echo % is the number being processed
1 is the number being processed
2 is the number being processed
3 is the number being processed
4 is the number being processed

我错过了什么?

xargs
  • 3 3 个回答
  • 2591 Views

3 个回答

  • Voted
  1. Best Answer
    dusty
    2013-06-19T04:41:54+08:002013-06-19T04:41:54+08:00

    做

    echo -e 1 2 3 4 | sed -e 's/\s\+/\n/g' | xargs --max-procs=3 --max-args=1 --replace=% echo % is the number being processed
    

    完成任务?输出似乎是正确的:

    1 is the number being processed
    2 is the number being processed
    3 is the number being processed
    4 is the number being processed
    

    我还尝试替换echo为sleep以确认它并行执行,并且确实如此:

    echo -e 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 9 9 9 9 | sed -e 's/\s\+/\n/g' | xargs --max-procs=20 --max-args=1 --replace=% sleep 1
    
    • 2
  2. StefanKaerst
    2021-08-03T03:22:56+08:002021-08-03T03:22:56+08:00

    AFAIK xargs 的默认输入分隔符是 \r ,因此您必须将其更改为 <space> 并相应地发送输入结尾,如下所示:

    $ echo -n "1 2 3 4 " | xargs -d" " --max-procs=3 --max-args=1 --replace=% echo % is the number being processed
    1 is the number being processed
    2 is the number being processed
    3 is the number being processed
    4 is the number being processed
    $ 
    

    HTH

    • 1
  3. Pablo A
    2020-04-05T16:46:01+08:002020-04-05T16:46:01+08:00

    其他一些方法,也许更简单,也可以使用-n, -max-args:

    echo -n "foo bar baz" | tr ' ' '\n' | xargs -n 1 echo    # With \n, no need for -0
    echo -n "foo bar baz" | tr ' ' '\0' | xargs -0 -n 1 echo # Using null character
    echo -n "foo bar baz" | xargs -d ' ' -n 1 echo           # Telling to xargs the delimiter
    
    • 哪里当然echo可以是你想要的任何命令。
    • 另见-L参数和-d '\n'(如果行有空格)。
    • Process win 与 并行--max-procs或可能更好parallel。
    • 0

相关问题

  • 换行符分隔的 xargs

  • 想知道为什么 sed 不能像我预期的那样使用 xargs

  • xargs --max-proc 拆分每个 proc 的输出?

  • 应用多个 .patch 文件

  • 是否构建了 svn 每晚提交脚本来捕获所有可能的场景?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve