0

I am trying to use a java script/software that is run by doing:

java -jar picard.jar MergeSamFiles \
      I=input_1.bam \
      I=input_2.bam \
      O=output_merged_files.bam

where I= represents the input into the program. I have a text file with inputs that I would like to feed into this program, such as:

arg1
arg2
arg3
...

where each line gets converted into I=arg1 I=arg2 I=arg3 .... Is there an easy way to do this in bash?

3 Answers 3

2

I suggest to use an array:

# read file.txt to array
mapfile -t array < file.txt

# prefix every array element with "I="
java -jar picard.jar MergeSamFiles \
      "${array[@]/#/I=}" \
      O=output_merged_files.bam
Sign up to request clarification or add additional context in comments.

Comments

1

In plain bash:

java -jar picard.jar MergeSamFiles \
    $(IFS=$'\n'; printf "I=%s " $(<inputfile)) \
    O=output_merged_files.bam

or alternatively

java -jar picard.jar MergeSamFiles \
    $(while read -r; do echo "I=$REPLY"; done < inputfile) \
    O=output_merged_files.bam

or using sed:

java -jar picard.jar MergeSamFiles \
    $(sed 's/^/I=/' inputfile) \
    O=output_merged_files.bam

Comments

0

I think something like this might work:

java -jar picard.jar MergeSamFiles $(sed -E 's/^/I=/' make_it_so.txt | xargs) O=output_merged_files.bam

If your input file has more args in it than xargs can flatten, you might run into some problems with the above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.