来自tsv的gnu并行输入

时间:2013-10-17 20:20:13

标签: python gnu gnu-parallel

我有3或4列的tsv,每列是shell脚本的参数 所以我想使用gnu parallel来运行带有来自tsv

的值的shell脚本

~ parallel --colsep "\t" thescript.py --arg1 {1} --arg2 {2} --arg3 {3} --arg4 {4} :::: input.tsv

第4列并不总是存在,所以我想知道只有在存在--arg4 {4}的情况下才能以智能方式添加{4}
python使用optparser.Optionparser,我更喜欢避免修改脚本。

1 个答案:

答案 0 :(得分:0)

当第4列没有值时,GNU Parallel将{4}作为字符串“{4}”传递。

您可以使用if:

包装thescript.py
parallel --colsep "\t" 'if [ "{4}" = "\{4\}" ]; then thescript.py --arg1 {1} --arg2 {2} --arg3 {3}; else thescript.py --arg1 {1} --arg2 {2} --arg3 {3} --arg4 {4}; fi'  :::: input.tsv

或者,如果您更喜欢可读性,请使用Bash功能:

my_func() {
    if [ "$4" = "\{4\}" ]; then
        thescript.py --arg1 $1 --arg2 $2 --arg3 $3
    else
        thescript.py --arg1 $1 --arg2 $2 --arg3 $3 --arg4 $4
    fi
}
export -f my_func
parallel --colsep "\t" my_func {1} {2} {3} {4} :::: input.tsv
相关问题