Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gnu parallel input from a tsv

i've a tsv of 3 or 4 columns, each column is an argument to a shell script.
so i want use gnu parallel to run the shell script with values from the tsv

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

the 4th column is not always present, so i was wondering if there is a smart way to add --arg4 {4} only if {4} exists.
the python uses optparser.Optionparser, i prefer to avoid modification to the script.

like image 644
robert laing Avatar asked Jun 29 '26 09:06

robert laing


1 Answers

GNU Parallel passes {4} as the string '{4}' when there is no value for column 4.

You could wrap thescript.py with an 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

Or if you prefer readability use a Bash function:

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
like image 168
Ole Tange Avatar answered Jun 30 '26 23:06

Ole Tange