Re: Newline Character Missing in Memory Variable

"[email protected] [sed-users]" <[email protected]> 21 May 2016 20:55:07 -0700
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
 This is not a "sed" problem, meaning, that the sed tool is not causing it.
 This is a purely shell phenomenon whereby the use of unquoted variables are enabling the word splitting behavior of the shell parser to kick in and mangle the variables. 
 FYI, it's not just those newlines that are disappear, all whitespace in your variables is also getting lost. The split happens on the contents of the IFS env variable, whose default unadulterated contents are a space, a TAB, and a newline.
 Once you lose the newlines and other spaces, there's no way to put them back as the information about their placement is gone with them. Your best bet is to always, without fail, quote all your variables in shell.
 

 

 

---In [email protected], <n22e113@...> wrote :

 Hello, Seders,
 File /tmp/file contains:
 Line 1
 Line 2
 Line 3
 Line 4
 Line 5
 
 $ sed -n '/^Line 2$/,/^Line 4$/p' /tmp/file returns
 Line 2
 Line 3
 Line 4
 
 However, spaces had replaced '\n':
 $ InMem=$(sed -n '/^Line 2$/,/^Line 4$/p' /tmp/file) && echo -e $InMem returns:
 Line 2 Line 3 Line 4
 
 and InMem='Line 2 Line 3 Line 4' and not
 InMem='Line 2\nLine 3\nLine 4'?
 
 Q1. Can I physically add '\n' in the sed command above?
 Q2. Why the difference in behaviour?
 Many thanks!
 Kwon



[Non-text portions of this message have been removed]