LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2008, 07:30 AM   #1
pppaaarrrkkk
Member
 
Registered: Jun 2005
Location: Aylesbury UK
Distribution: Red Hat enterprise linux 6
Posts: 101

Rep: Reputation: 15
sed - How do you replace end of line with a space


How do I turn :

one
two
three


into :


one two three
 
Old 04-22-2008, 07:51 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You don't really need sed (unless this is some kind of exercise). Just do
Code:
echo $(cat file)
 
Old 04-22-2008, 08:01 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
If you have a fixed number of lines, then you can do it in a one-line command. Suppose the filename is "nums":

sed 'N;N;s/\n/ /g" nums

This reads the first line, then appends two more lines (N;N. Then it substitutes a space for each newline.

My favorite SED tutorial here: http://www.grymoire.com/Unix/Sed.html
 
Old 04-22-2008, 08:06 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by colucix View Post
You don't really need sed (unless this is some kind of exercise). Just do
Code:
echo $(cat file)
That only works if the three lines in question are the only things in the file.

ppp;
We obviously would need the most general description of what you are trying to do.
 
Old 04-22-2008, 10:01 AM   #5
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
Here's your answer, replacing all newlines (\n) with a tab so you can easily see what it's doing.
Code:
sed -i ':a;N;$!ba;s/\n/\t/g' file
the "-i" is for in place editing.

Here's my explanation from a tutorial I wrote back in 2005.
Quote:
Breakdown:
; (semicolon is just a command separator within sed)
sed (The command that calls the sed program)
:a (a marker which we use to iterate over and over)
N (Appends the next line to the pattern space)
$!ba (repeats the N command for all lines but the last line)
s/\n/\t/g (replaces all occurences of the newline character with tab)

Last edited by WindowBreaker; 04-22-2008 at 10:04 AM.
 
Old 04-23-2008, 01:08 PM   #6
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Code:
tr '\n' ' ' <test.txt
Code:
awk '$1=$1' ORS=' ' test.txt
EDIT: I know it's not sed, but sed might not be the best tool for this job. I'm not sure sed has a way of doing this without reading the whole file into memory.

Last edited by angrybanana; 04-23-2008 at 01:12 PM.
 
Old 02-07-2011, 10:54 AM   #7
thiyagi
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Rep: Reputation: 0
its not working at all..
 
Old 02-07-2011, 11:27 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

instead of resurrecting a nearly three year old thread and simply stating that it is not working you should start a new one. Post what you are trying to do and what you have tried so far. Post error messages if there are any and/or the result that you get with your approach.
 
  


Reply



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
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 05:28 AM
[SOLVED] sed: How to remove the end of a line? angel115 Programming 2 10-01-2007 10:29 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 05:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration