Home > Software design >  Read list of comma separated strings from file and execute command on each string in Bash
Read list of comma separated strings from file and execute command on each string in Bash

Time:03-29

I have a list of comma-separated strings in a file as follows: The input file sentences.txt contains:

'a1', 'b2', 'c3', 'd4'

On running a script run_script.sh on each of the above strings, a corresponding output for each input is obtained.

The output file output.txt contains:

'A1', 'B1', 'C1', 'D1'

For example, running the script run_script.sh on a single string is like:

bash run_script.sh a1 > prediction.txt

I want loop through each element of the file sentences.txt run the script run_script.sh and store the output of the script in prediction.txt.

How can I write an effective bash script to do that?

I tried to read the file as suggested here, but didn't get any output.

CodePudding user response:

Sounds like homework. :)
Or maybe an XY Problem...

First, why is c3 becoming C1? Is that just a typo?

IF SO, then simplistically, to uppercase each line -

$: declare -u line; while read -r line; do echo "$line"; done < sentences.txt
'A1', 'B2', 'C3', 'D4'

declare -u line declares the variable line as always uppercasing its content. while read -r line; do cycles through lines from STDIN assigning each to line. echo "$line" literally just prints the (now uppercased) value. done < sentences.txt closes the loop and assigns the file as its source for STDIN.

Even simpler (and faster),

tr '[[:lower:]]' '[[:upper:]]' < sentences.txt > prediction.txt

However, if you are supposed to read each "word" in your "sentences" individually, and those outputs are more complex than just a typo on uppercasing (which is likely if you are doing genome work...) then you need some more complex parsing rules.

  • Are you SURE there can be no embedded commas?
  • Are commas ALWAYS followed by a (single?) Space character?
  • Are ALL fields ALWAYS single-quoted?
  • Are there EVER any embedded spaces within a field?
  • Does the script expect the single-quotes and commas embedded in its input?

etc. These kinds of things can crash your parse, obviously.
Still, assuming a simplest case, where all these assumptions are valid, there are still a few reasonably easy ways to handle it.

while read -ra words; do 
  for word in "${words[@]}"; do
    run_script.sh "$word"
  done
done < sentences.txt > prediction.txt

On the other hand, if those quotes and commas are not supposed to be considered part of the data, you may have a little more headache...

There are a few other details to consider, too.

bash run_script.sh a1 > prediction.txt
I want loop through each element of the file sentences.txt run the script run_script.sh and store the output of the script in prediction.txt.

This is going to overwrite the file every time, and the only thing in it when you are done will be the output from the very last operation. In the very least, you need >> to append, and some logic that prevents newlines until you reach EOL.

Can you give us a little more to work with?

  • Related