Home > OS >  How can I parse stdout line by line in C shell?
How can I parse stdout line by line in C shell?

Time:09-25

If I have a script (script.csh) that prints to stdoud, like:

echo "First line"
echo "Second line"
echo "Third line"

And I try to parse that script's output from another script as such:

foreach line (`script.csh`)
    echo $line
end

The the output is delimited by spaces instead of new lines:

First
line
Second
line
Third
line

How can I fix it so that I'm reading in one line at a time? And I know that C shell is not recommended, but that's what my boss wanted me to use.

CodePudding user response:

You have just to double quotes the script output:

#!/usr/bin/env csh
foreach line ("`script.csh`")
   echo $line
end

The result will be:

$ ./parser.csh 
First line
Second line
Third line
  • Related