I have a file input.txt with three columns


I want to read the first two fields, perform some function and name the output with the last field.

while IFS=" " read -r f1 f2 f3; do cat $f1 $f2 > $f3; done < file.txt

How can I apply this to longer codes

the input.sample.text has following three columns

../Data/G20P1sc-C05-R1.fastq.gz ../Data/G20P1sc-C05-R1.fastq.gz G20P1sc-C05
../Data/G20P1sc-C08-R1.fastq.gz ../Data/G20P1sc-C08-R1.fastq.gz G20P1sc-C08
 while IFS=" " read -r f1 f2 f3; do rsem-calculate-expression --star --star-path /share/pkg/star/2.7.0e/bin --temporary-folder --star-gzipped-read-file --paired-end $f1 $f2 ../genome_indices/rsem-star/rsem-star ../test_results/$f3; done < src_pdm3012/input.sample.text 

The output has the following error

EXITING: FATAL INPUT ERROR: empty value for parameter "outFileNamePrefix" in input "Command-Line-Initial"
SOLUTION: use non-empty value for this parameter

I try to explicitly use the --outFileNamePrefix and it still has the same error

while IFS=" " read -r f1 f2 f3; do rsem-calculate-expression --star --star-path /share/pkg/star/2.7.0e/bin --temporary-folder --star-gzipped-read-file --paired-end $f1 $f2 ../genome_indices/rsem-star/rsem-star --outFileNamePrefix $f3; done < ../src_pdm3012/input.sample.text
  • 2
    What is your question exactly?
    – Sparhawk
    May 30, 2019 at 22:44
  • 1
    What do you mean by "longer codes"?
    – Kusalananda
    May 30, 2019 at 22:44
  • I was hoping I could apply this to mapping multiple fastq files and naming the output.
    – PDM
    May 30, 2019 at 22:45
  • Something Like this: while IFS=" " read -r f1 f2 f3; do rsem-calculate-expression --star --star-path /share/pkg/star/2.7.0e/bin --temporary-folder --star-gzipped-read-file --paired-end $f1 $f2 ../genome_indices/rsem-star/rsem-star --outFileNamePrefix $f3; done < ../src_pdm3012/input.sample.text But it does not work
    – PDM
    May 30, 2019 at 22:46
  • 3
    How does it "not work"? What are the error messages? I'd suggest editing your question and putting this very relevant information in it.
    – Sparhawk
    May 30, 2019 at 22:49

2 Answers 2


I suggest awk script to do this:

awk '{ print $1,$2 > $3}'

Of course you must be sure that $3 exists (and is unique) and that there is no confution what the third column is, i.e. columns 1, 2, and 3 should not contain field separators.

awk will also allow you manyfold manipulations on the first two columns.

  • It's not clear what this does that the code in the question does not do.
    – Kusalananda
    May 31, 2019 at 9:04
  • @Kusalananda "longer codes" and "perform some function" in the question is a bit unspecific. It could well be a problem fitting for awk with this answer just showing how to get a column as per-line filename for the output.
    – FelixJN
    May 31, 2019 at 9:26
  • this code write line field $1 and line field $2 into output file named by line field $3
    – Dudi Boy
    May 31, 2019 at 19:55

You can redirect a command group or any form of compound command to redirect the output of more than one command, whether it's a while loop or not:

while IFS=" " read -r f1 f2 f3; do
    cat < "$f1"
    echo whatever
    cat < "$f2"
    echo end
  } > "$f3"
done < file.txt

Also remember that in POSIX shells, variables expansions need to be quoted.

  • @Stéphan..can you show how to write the above code for rsem-calculate-expression in while loop ? I guess, the difference from your answer in format is , in my code the output will be within the brackets.
    – PDM
    May 31, 2019 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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