将 Xargs max-procs 与文件中的多个参数一起使用

Using Xargs max-procs with multiple arguments from a file

我有一个脚本可以得到我想要的结果。我想提高脚本的性能。

我的脚本从文件 file1.txt.

获取参数

内容如下:

table1
table2
table3
and so on

现在,当我按顺序使用脚本下面的 while 语句时 运行s。

while声明如下:

while IFS=',' read -r a; do import.sh "$a"; done <  file1.txt

现在,当我在 bash 中使用 xargs max-procs 实用程序时,脚本 运行 基于 max-procs 的数量并行运行。

xargs声明如下:

xargs --max-procs 10 -n 1 sh import.sh <  file1.txt

现在我有另一个脚本

此脚本从文件 file2.txt.

获取参数

内容如下:

table1,db1
table2,db2
table3,db3
and so on

当我使用 while 语句时脚本执行良好。

while IFS=',' read -r a b; do test.sh "$a" "$b"; done <  file2.txt

但是当我使用 xargs 语句时,脚本会给我 usage 错误。

xargs声明如下。

xargs --max-procs 10 -n 1 sh test.sh <  file2.txt

error声明如下:

Usage : test.sh input_file

为什么会这样?

我该如何纠正这个问题?

您的第二个脚本 test.sh 需要两个参数,但 xargs 只提供一个参数(一个词,在本例中为整行)。您可以通过首先将逗号 , 转换为换行符(使用简单的 sed 脚本)然后在每次调用 test.sh (使用 -n2):

sed s/,/\n/g file2.txt | xargs --max-procs 10 -n2 sh test.sh

请注意,xargs 通过 -d 选项支持自定义分隔符,您可以使用它以防 file2.txt 中的每一行都以 , 结尾(但随后您可能应该去掉每个第一个字段的前缀换行符)。