2013-10-17 29 views
1

我有一個tsv爲3或4列,每列是一個shell腳本的參數。
所以我要使用GNU平行於從TSV從tsv的gnu並行輸入

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

第4列並不總是存在運行與價值觀的shell腳本,所以我想知道是否有添加--arg4 {4}只有當一個聰明的辦法{4}存在。
python使用optparser.Optionparser,我寧願避免修改腳本。

回答

0

GNU平行通道{4}爲字符串 '{4}' 時,有列4

沒有價值,您可以換thescript.py使用if:

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