LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash; awk or sed output to variable: how keep newline at end of each output line (https://www.linuxquestions.org/questions/programming-9/bash%3B-awk-or-sed-output-to-variable-how-keep-newline-at-end-of-each-output-line-885664/)

porphyry5 06-10-2011 05:28 PM

Bash; awk or sed output to variable: how keep newline at end of each output line
 
Either of
Code:

txt=$(sed -n -e "${ln[0]},${ln[1]} p" -e "${ln[1]} q" Notes)
txt=$(awk -v x=${ln[0]} -v y=${ln[1]} 'NR>x, NR<y' < Notes)

does what I want except all the newlines are removed if their output goes to a variable. How can I preserve them?

colucix 06-10-2011 05:37 PM

Have you tried to embed the variable in double quotes?
Code:

echo "$txt"

porphyry5 06-10-2011 05:46 PM

Quote:

Originally Posted by colucix (Post 4382311)
Have you tried to embed the variable in double quotes?
Code:

echo "$txt"

Aah, thank you. So they are there, its just that echo $txt shows line breaks as spaces.

colucix 06-10-2011 05:50 PM

This is the way echo works: it outputs the arguments separated by spaces. Multiple lines are multiple arguments separated by newlines, whereas using double quotes multiple lines are treated as a single argument containing newlines and it is printed as is.


All times are GMT -5. The time now is 05:11 PM.