shellcsh

how to iterate every line from variable on the basis of new line character instead of space in csh


#!/bin/csh -f

foreach line (`cat test.txt`)
  echo "$line"
end

test.txt file contains following content :

How to Ask

Is your question about programming?

We prefer questions that can be answered, not just discussed.

Provide details. Share your research.

If your question is about this website, ask it on meta instead.

But on running the above csh script i am getting following output which prints on the basis of space delimiter instead of new line

./test.csh

output is in file enter image description here


Solution

  • You can use this;

    #!/bin/csh -f
    foreach line ( "`cat test.txt`" )
      echo $line
    end
    

    Test :

    $ csh test.csh 
    How to Ask
    Is your question about programming
    We prefer questions that can be answered, not just discussed.
    Provide details. Share your research.
    If your question is about this website, ask it on meta instead.