4

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?

Philippos
  • 13,237
  • 2
  • 37
  • 76
The69Er
  • 87
  • 4

2 Answers2

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

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
jesse_b
  • 35,934
  • 12
  • 91
  • 140
  • 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 '19 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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250