LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
Search this Thread
Old 06-08-2007, 01:55 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 206Reputation: 206Reputation: 206
sed insert newline character


Is there any way to add a newline character with sed? If not sed, another utility?

I would like to do something like
Code:
echo "word1a word2a " | sed 's|a |\n|'
but the above doesn't actually add a newline.
 
Old 06-08-2007, 02:03 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

What did you actually try? It does work on my box:

Code:
$ echo "word1a word2a "
word1a word2a 
$ echo "word1a word2a " | sed 's|a |\n|'
word1
word2a 
$
 
Old 06-08-2007, 02:12 PM   #3
Marsolin
Member
 
Registered: Jul 2005
Posts: 124

Rep: Reputation: 15
Where do you want the newline? Between the words or somewhere else?
 
Old 06-08-2007, 02:20 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
What is wrong with just doing:
Code:
echo "word1\nword2\n"
 
Old 06-08-2007, 02:52 PM   #5
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 206Reputation: 206Reputation: 206
Quote:
What did you actually try? It does work on my box:
Fancy that. Yea... sorry, it works on my box too. I guess what I meant to ask was how to get future instances to be replaced too... like the second a. Thanks
 
Old 06-08-2007, 03:11 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

You make it a global search and replace:

echo "word1a word2a " | sed 's|a |\n|g'

Code:
$ echo "word1a word2a " | sed 's|a |\n|g'
word1
word2a 

$
Hope this helps.
 
Old 06-08-2007, 03:13 PM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
You need a "g" for it to globally replace the string.

Code:
echo 'word1a word2a ' | sed 's|a |\n|g'

Edited: Well, it looks like druuna beat me to it.

Last edited by forrestt; 06-08-2007 at 03:14 PM.
 
  


Reply

Tags
newline, sed, substitution


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed: insert a newline. Why does not it work? J_Szucs Linux - Software 4 03-19-2014 01:28 PM
Insert character by using sed/awk manish_meet_in Linux - General 3 04-05-2007 12:19 PM
Insert after every nth character using sed DoDo1975 Linux - Software 2 01-17-2007 06:59 PM
Need to Insert Newline Char amh06 AIX 6 09-15-2006 02:35 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 11:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration