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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With