4

This question already has an answer here:

I have a file which has two file names per row, like this:

file1.fastq.gz file2.fastq.gz
...
file9fastq.ga file10fastq.gz

How can I pass the two names as arguments for a script?

marked as duplicate by muru, Fabby, BitsOfNix, Philippos, Michael Homer Jul 30 at 6:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Each line of the input file should be use as parameters to the script? – RonJohn Jul 26 at 2:22
8

Using a while read loop:

while read -r file1 file2 trash; do
  something with "$file1" and "$file2"
done < /path/to/input_file

This will read your input file line by line, setting file1 and file2 with the first and second columns respectively. trash is probably unnecessary but I like to include it to handle things you may encounter such as:

file1.fastq.gz file2.fastq.gz foo

If your file contained a line like the above and you did not include the trash variable (or one similar), your file2 variable would be set to: file2.fastq.gz foo

  • I tried while read -r file1 file2 trash; do BatchTester $file1 $file2 done < configseparated but instead of executing it what I get is space too keep writing instead of execution however the sheel then only o gives me the opportunity to keep writing – The69Er Jul 30 at 17:45
4

xargs is another way, if your file format is exactly as you say: two whitespace-separated fields per line (noting the ... ellipsis).

< input xargs -n2 ./script-here

... will read the file named input line-by-line and pass two arguments to ./script-here.

You can rearrange the redirection:

xargs -n2 ./script-here < input

... if it makes better intuitive sense to you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.