LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 12-08-2005, 11:42 AM   #1
bkeeper
LQ Newbie
 
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25

Rep: Reputation: 16
Cool Replacing new lines (\n) from a file


Hi there!

This sounds like such a simple task, yet I haven't succeeded so far... I have some standard, not-so-long linux text file, say, readme.txt, and I want to delete all new lines (\n). How can this be so difficult?

I tried
Code:
cat readme.txt | sed s/"\n"/""/
but this doesn't work. I guess, that's because sed reads only one line per go? So I could use
Code:
cat readme.txt | sed 'N;s/\n//'
which would join adjacent lines all right. But, of course, to replace all line breaks, I would have to do this more often
Code:
cat readme.txt | sed 'N;s/\n//' | sed 'N;s/\n//' | sed 'N;s/\n//' | ...
or even put a loop around it. But this seems so difficult! Ain't there any better way?

(actually, what I want to do is to replace the standard "You have Mail in /var/spool/..." message by "You have Mail from Tom, Andi, Sue". So I'm looking for a fast one-liner )

Cheers,
Bkeeper
 
Old 12-08-2005, 12:42 PM   #2
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
For not-so-long linux text file, you can do:

VAR=`cat readme.txt` && echo $VAR

you get one long concatenated line where the \n are replaced by a blank character.

to remove this space, you have to mark the ends of line, like that:

VAR=`sed 's/$/$$/' readme.txt` && echo $VAR ' '|sed 's/\$\$ //'g

Last edited by berbae; 12-08-2005 at 01:15 PM.
 
Old 12-09-2005, 02:06 AM   #3
bkeeper
LQ Newbie
 
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25

Original Poster
Rep: Reputation: 16
Yes, that works!
Thank you very much.
Cheers,
BKeeper
 
Old 12-14-2005, 09:13 PM   #4
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
Here's the sed command you need

here's the sed command you were looking for:

sed ':a;N;$!ba;s/\n//g' <inputfile>

The ':a' is a marker.
The '$!ba' causes sed to branch to the ':a' marker for all lines except the last line. This creates one huge line.
Finally, all \n's are removed.

This took me a while to find, but it's one of those things you'll want to keep around.

Pablo
 
Old 12-15-2005, 02:13 AM   #5
bkeeper
LQ Newbie
 
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25

Original Poster
Rep: Reputation: 16
Hi Pablo,
great!
Thanks for you post. It'll take me a bit to understand the expression, but, well, you know, with sed it's always worth the effort...
Cheers,
BKeeper
 
  


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
replacing text in file kushalkoolwal Programming 17 10-25-2005 05:25 AM
Replacing a particular position in a file sgracelin Linux - Newbie 14 06-23-2005 06:40 AM
Replacing a particular position in a file sgracelin Linux - Newbie 1 06-20-2005 05:54 PM
replacing characters in a file with others pcdude Linux - General 6 01-19-2005 12:29 AM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:04 PM.

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