How to take substring from input file as an argument to a program to be executed in GNU-parallel?

為{幸葍}努か 提交于 2019-12-11 00:39:24

问题


I am trying to execute a program (say, biotool) using GNU-parallel which takes 3 arguments, i, o and a :

  • the input files (i)
  • output file name to be written in (o)
  • an argument which takes a sub string from the input file name (a)

for example, say i have 10 text files like this

1_a_test.txt
2_b_test.txt
3_c_test.txt
...
10_j_test.txt

I want to run my tool (say biotool) on all the 10 text files. I tried this

parallel biotool -i {} -o {.}.out -a {} ::: *.txt

I want to pass the charachter/letter/whatever before the first underscore from the input text file name as an argument to -a option like this (dry run):

parallel biotool -i 1_a_test.txt -o 1_a_test.out -a 1 ::: *.txt`
parallel biotool -i 2_b_test.txt -o 2_b_test.out -a 2 ::: *.txt`
parallel biotool -i 3_c_test.txt -o 3_c_test.out -a 3 ::: *.txt`
...

{} supplies the complete file name to -a but I only want the sub string before the first underscore to be supplied to -a


回答1:


The easiest, but harder to read is this:

parallel --dry-run biotool -i {} -o {.}.out -a '{= s/_.*// =}'  ::: *test.txt

Alternatively, you can make a bash function that uses bash Parameter Substitution to extract the part before the underscore. Then export that to make it known to GNU Parallel

#!/bin/bash

doit(){
  i=$1
  o=$2
  # Use internal bash parameter substitution to extract whatever precedes "_" 
  # See https://www.tldp.org/LDP/abs/html/parameter-substitution.html
  a=${i/_*/}
  echo biotool -i "$i" -o "$o" -a "$a"
}

export -f doit

parallel doit {} {.}.out ::: *test.txt

Sample Output

biotool -i 10_j_test.txt -o 10_j_test.out -a 10
biotool -i 1_a_test.txt -o 1_a_test.out -a 1
biotool -i 2_b_test.txt -o 2_b_test.out -a 2


来源:https://stackoverflow.com/questions/57705353/how-to-take-substring-from-input-file-as-an-argument-to-a-program-to-be-executed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!