Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-17-2004, 01:46 AM
|
#1
|
Senior Member
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126
Rep:
|
Sed: insert a newline. Why does not it work?
I want to replace "," by newline in each line, and I tried this:
echo "first,second,third" | sed 's/,/\n/g'
It poduced this:
firstnsecondnthird
(no newlines there :-()
So I tried this:
echo "first,second,third" | sed 's/,/'"$(printf '\n')"'/g'
Which produced this:
firstsecondthird
(still no newlines there :-(((((()
There are millions of sed examples like this on the internet, so why dos not it work for me?
Is there a workaround?
Last edited by J_Szucs; 03-17-2004 at 02:01 AM.
|
|
|
03-17-2004, 02:52 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
It does work if you do it like this:
echo "one,two,three" | sed 's/,/\
/g'
After the backslash you just hit return, you will get the PS2 prompt (probably >) and continue with the /g' part.
From within a script I do like this sollution, when using this from the command line I do like this one better:
echo "first,second,third" | awk 'BEGIN { FS=","; OFS="\n" } { print $1, $2, $3 }'
Hope this helps.
|
|
|
03-17-2004, 04:48 AM
|
#3
|
Senior Member
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126
Original Poster
Rep:
|
Well, your example with sed really works from the command line, but, actually I want to use it from a script.
(Only it was easier to test possible commands from the command line)
The awk command would be OK, but that presumes that there are always 3 fields ($1, $2,$3), while in my case there can be any number of fields on the line.
In the meantime I found that tr is the best to it:
echo "first,second,third" | tr ',' '\n'
Result:
first
second
third
It is just what I want.
|
|
|
04-11-2004, 08:39 PM
|
#4
|
LQ Newbie
Registered: Apr 2004
Posts: 1
Rep:
|
Try this instead:
echo "one,two,three" | sed "s/,/\\`echo -e '\n\r'`/g"
|
|
|
03-19-2014, 01:28 PM
|
#5
|
Member
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
|
Resurrecting Thread w/ the "Why"
Greetingz!
I'm 'resurrecting' this thread (read: posting to a very old thread) because I keep coming across this in google searches.
I'm going to treat this as a two-part Question;
1st Question: How can I replace a character with a newline using sed?
2nd Question: Why doesn't sed use \n to denote a newline in a substitution expression?
Answer for "How":
On a Linux system, the examples OP is seeing everywhere work.
However, sed on an actual UNIX (or BSD) system like Solaris & HP-UX can behave differently.
The GNU version of 'sed' (4.1.5 circa 2003, for me) handles '\n' as you would expect.
Using Red Hat Enterprise Linux 5 (Update 9)
xeleema @ Linux $ echo "one,two,three"|sed -e 's/,/\n/g'
one
two
three
xeleema @ Linux $ However something like Solaris (Release 10/08 Update 6), this gives what the OP is getting;xeleema @ SunOS $ echo "one,two,three"|sed -e 's/,/\n/g'
onentwonthree
xeleema @ SunOS $ For Solaris, you have two options;
1) Embed a newline the old-fashioned way (this is good at the command-line, but not so much when doing this in a script);
Note that you have to basically press enter right after the backslash (some shells don't require a backslash).
Also, the ">" character is the default 'PS2' prompt (see the man page for ksh or bash).
xeleema @ SunOS $ echo "one,two,three" | sed -e 's/,/\
> /g'
one
two
three
xeleema @ SunOS $
2) Use the 'tr' command if you are replacing just a single character;
xeleema @ SunOS $ echo "one,two,three" | tr ',' '\n'
one
two
three
xeleema @ SunOS $ Answer for "Why":
Early versions of 'sed' (and implementations that copied them) did not originally accept escaped characters in the Right-Hand-Side (RHS).
Information on this can be found in SourceForge's 'sed FAQ' online.
I've been unable to track down more information as to 'why' sed doesn't like dealing with '\n', I suspect Mr. McMahon might have been able to give us more guidance, had he not passed away in 1989.
The fact that sed has been around since about 1973 could have something to do with it.
I've seen older code with limitations built-in that reflected the current pre-existing limitations of various parts of an Operating system.
Once the Operating System improves, someone else has to go back and patch (or outright fork) the code and write-in improvements.
I'd imagine that's what the maintainers of GNU sed eventually wound up doing.
|
|
1 members found this post helpful.
|
06-14-2019, 08:18 AM
|
#6
|
LQ Newbie
Registered: Dec 2015
Posts: 2
Rep:
|
The literally-correct solution
Resurrecting this long-dead thread because it still ranks highly on Google search for this topic.
Having programmed Bash for a few decades, this type of question recurs often amongst Bash users, because the correct solution is not obvious to casual users.
While it's true that the creative use of GNU command-line tools can be used to insert a literal newline into the middle of a command like sed, the truth is that one can simply use Bash literal strings instead.
Here's how to represent newline from the Bash command line:
$'\n'
Yep, it's that simple:
Code:
$ echo $'Hello,\nWorld'
Hello,
World
or:
Code:
$ echo "Hello,"$'\n'"World"
Hello,
World
Bash string literals are not interpreted within interpolated strings, so the following will not work similarly:
Code:
$ echo "Hello,$'\n'World"
Hello,$'\n'World
Last edited by dejayc; 06-14-2019 at 08:19 AM.
|
|
|
All times are GMT -5. The time now is 09:55 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|