使用 GNU 并行分割命令行参数

发布于 2024-11-13 09:25:54 字数 591 浏览 1 评论 0原文

使用GNU并行http://www.gnu.org/software/parallel /

我有一个需要两个参数的程序,例如

$ ./prog file1 file2
$ ./prog file2 file3
...
$ ./prog file23456 file23457

我正在使用生成文件名对的脚本,但这会带来问题,因为脚本的结果是单个字符串 - 而不是一对。像:

$ ./prog "file1 file2"

GNU并行似乎有很多技巧,我想知道是否有一个用于围绕分隔符分割文本的技巧:

$ generate_file_pairs | parallel ./prog ?  
  # where ? is text under consideration, like "file1 file2"

简单的解决方法是在prog中手动分割参数,但我会想知道 GNU 并行是否可能。

Using GNU parallel: http://www.gnu.org/software/parallel/

I have a program that takes two arguments, e.g.

$ ./prog file1 file2
$ ./prog file2 file3
...
$ ./prog file23456 file23457

I'm using a script that generates the file name pairs, however this poses a problem because the result of the script is a single string - not a pair. like:

$ ./prog "file1 file2"

GNU parallel seems to have a slew of tricks up its sleeves, I wonder if there's one for splitting text around separators:

$ generate_file_pairs | parallel ./prog ?  
  # where ? is text under consideration, like "file1 file2"

The easy work around is to split the args manually in prog, but I'd like to know if it's possible in GNU parallel.

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

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

发布评论

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

评论(4

人│生佛魔见 2024-11-20 09:25:54

您可能正在寻找--colsep

generate_file_pairs | parallel --colsep ' ' ./prog {1} {2}  

阅读manparallel了解更多信息。如果您还没有观看介绍视频,请观看http://www.youtube.com/watch?v=OpaiGYxkSuQ< /a>

You are probably looking for --colsep.

generate_file_pairs | parallel --colsep ' ' ./prog {1} {2}  

Read man parallel for more. And watch the intro video if you have not already done so http://www.youtube.com/watch?v=OpaiGYxkSuQ

少女的英雄梦 2024-11-20 09:25:54

很晚了,但我经常遇到这个问题,并找到了一个很好的简单解决方案,

在将 arg 列表传递给并行之前,只需用换行符替换所有空格即可。我发现 tr 对于此类内容来说是最快的

不工作

echo "1 2 3 4 5"  | parallel echo --
-- 1 2 3 4 5

工作

echo "1 2 3 4 5" | tr ' ' '\n' | parallel echo --
-- 1
-- 2
-- 3
-- 4
-- 5

Protip:早于 实际上运行并行命令时,我做了两件事来检查参数是否已正确分割。

  1. 在 bash 命令前面添加 echo 。这意味着最终将执行的任何命令都将被打印出来供您首先检查
  2. 在回显中添加一个标记,这将检查并行分割是否实际工作

>请注意,这对于小/中参数列表最有效。如果参数列表非常大,可能最好只使用 for 循环来回显每个参数以并行

Quite late to the party here, but I bump into this problem fairly often and found a nice easy solution

Before passing the arg list to parallel, just replace all the spaces with newlines. I've found tr to be the fastest for this kind of stuff

Not working

echo "1 2 3 4 5"  | parallel echo --
-- 1 2 3 4 5

Working

echo "1 2 3 4 5" | tr ' ' '\n' | parallel echo --
-- 1
-- 2
-- 3
-- 4
-- 5

Protip: before actually running the parallel command, I do 2 things to check that the arguments have been split correctly.

  1. Prepend echo in front of your bash command. This means that any commands that will eventually be executed will be printed for you to check first
  2. Add a marker in the echo, this checks that the parallel split is actually working

> Note, this works best with small/medium argument lists. If the argument list is very large, probably best to just use a for loop to echo each argument to parallel

无戏配角 2024-11-20 09:25:54

您正在寻找并行的 -n 选项。这就是您要查找的内容:

./generate_file_pairs | parallel -n 2 ./prog {}

摘自 GNU 并行文档

-n max-args
    Use at most max-args arguments per command line. Fewer than max-args 
    arguments will be used if the size (see the -s option) is exceeded, 
    unless the -x option is given, in which case GNU parallel will exit.

You are looking for -n option of parallel. This is what you are looking for:

./generate_file_pairs | parallel -n 2 ./prog {}

Excerpt from GNU Parallel Doc:

-n max-args
    Use at most max-args arguments per command line. Fewer than max-args 
    arguments will be used if the size (see the -s option) is exceeded, 
    unless the -x option is given, in which case GNU parallel will exit.
暖树树初阳… 2024-11-20 09:25:54

Parallel 的手册中说:

如果没有给出命令,则执行输入行...GNU Parallel 通常可以用作 xargs 或 cat | 的替代品。 bash。

因此,尝试一下:

generate command | parallel

尝试理解以下输出:

for i in {1..5};do echo "echo $i";done | parallel

In Parallel's manual, it is said:

If no command is given, the line of input is executed ... GNU parallel can often be used as a substitute for xargs or cat | bash.

So take a try of:

generate command | parallel

Try to understand the output of this:

for i in {1..5};do echo "echo $i";done | parallel
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文