bash - 更改 xargs(或 GNU Parallel)参数中的文本

标签 bash sed awk parallel-processing xargs

我有一个程序,可以通过两种方式运行:单端或成对端模式。语法如下:

program <output-directory-name> <input1> [input2]

其中需要输出目录和至少一个输入。如果我想在三个文件(例如示例 A、B 和 C)上运行此命令,我会使用类似 find 和 xargs 或并行的方法:

user@host:~/single$ ls
sampleA.txt  sampleB.txt  sampleC.txt

user@host:~/single$ find . -name "sample*" | xargs -i echo program {}-out {}
program ./sampleA.txt-out ./sampleA.txt
program ./sampleB.txt-out ./sampleB.txt
program ./sampleC.txt-out ./sampleC.txt

user@host:~/single$ find . -name "sample*" | parallel --dry-run program {}-out {}
program ./sampleA.txt-out ./sampleA.txt
program ./sampleB.txt-out ./sampleB.txt
program ./sampleC.txt-out ./sampleC.txt

但是当我想以“paired-end”模式运行程序时,我需要给它两个输入。这些是相关文件,但它们不能简单地连接起来 - 您必须以这两个文件作为输入来运行程序。文件的命名合理,例如sampleA_1.txt 和sampleA_2.txt。

我希望能够使用 xargs(或最好是并行)之类的东西在命令行上轻松创建它:

user@host:~/paired$ ls
sampleA_1.txt  sampleB_1.txt  sampleC_1.txt
sampleA_2.txt  sampleB_2.txt  sampleC_2.txt

user@host:~/paired$ find . -name "sample*_1.txt" | sed/awk? | parallel ?
program ./sampleA-out ./sampleA_1.txt ./sampleA_2.txt
program ./sampleB-out ./sampleB_1.txt ./sampleB_2.txt
program ./sampleC-out ./sampleC_1.txt ./sampleC_2.txt
理想情况下,该命令会删除 _1.txt 以创建输出目录名称(sampleA-out 等),但我确实需要能够采用该参数并将第二个输入的 _1 更改为 _2 .

我知道这对于脚本来说非常简单 - 我在 Perl 中使用快速正则表达式替换来完成此操作。但我希望能够通过快速的一句台词来做到这一点。

提前致谢。

最佳答案

I did this in Perl with a quick regular expression substitution. But I would love to be able to do this with a quick one-liner.

Perl 也有单行语句,就像 sedawk 一样。你可以写:

find . -name "sample*_1.txt" | perl -pe 's/_1\.txt$//' | parallel program {}-out {}_1.txt {}_2.txt

(-e 标志表示“下一个参数是程序文本”;-p 标志表示“程序应循环运行;对于每一行输入,将 $_ 设置为该行,然后运行程序,然后打印 $_"。)

关于bash - 更改 xargs(或 GNU Parallel)参数中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9688659/

相关文章:

linux - 正则表达式在通过 ssh 的 sed 中不被接受

linux - Awk 命令无法正常工作,输出错误,sed 命令?

awk - 如何在awk中声明一个数组

linux - Bash 脚本 - 字符串比较似乎不起作用

python - 在写入其标准输入后后台进程

bash - 用sed插入n个重复字符

shell - 在 .csv 文件中使用 shell 脚本在逗号上拆分字符串但忽略双引号内的逗号?

c++ - 查找 Valgrind PID 以调用应用程序

bash - 如何在 wget 请求中显示缺少前导 0?

regex - 查找和替换以模式开头的行