Discussion:
Newline Character Missing in Memory Variable
n22e113 n22e113@yahoo.ca [sed-users]
2016-05-21 15:36:36 UTC
Permalink
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
Jim Hill gjthill@gmail.com [sed-users]
2016-05-21 15:42:43 UTC
Permalink
The shell splits unquoted variable substitutions into arguments at
whitespace (controlled by the IFS variable, you can use anything else you
want), and `echo` echoes its arguments separated by spaces. Either set IFS
empty or quote your substitutions.


[Non-text portions of this message have been removed]
Thierry Blanc Thierry.Blanc@gmx.ch [sed-users]
2016-05-21 15:43:08 UTC
Permalink
echo removes newline, if unquoted.

See the difference

$ $a="line 1
line 2
line3"
$ echo $a
$ echo "$a"
Post by n22e113 ***@yahoo.ca [sed-users]
Hello, Seders,
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
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
------------------------------------
------------------------------------
------------------------------------

------------------------------------
--
------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/sed-users/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/sed-users/join
(Yahoo! ID required)

<*> To change settings via email:
sed-users-***@yahoogroups.com
sed-users-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
sed-users-***@yahoogroups.com

<*> Your use of Yahoo Groups is subject to:
https://info.yahoo.com/legal/us/yahoo/utos/terms/
sharma__r@hotmail.com [sed-users]
2016-05-22 03:55:07 UTC
Permalink
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 sed-***@yahoogroups.com, <***@...> 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]

Loading...