LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-20-2006, 03:29 AM   #1
Fond_of_Opensource
Member
 
Registered: May 2006
Posts: 55

Rep: Reputation: 15
Exclamation removing new line character using sed


How to remove new line character using sed?

cat $myfile
Using my computer, you can prevent activities related to this info by using fun stuff of this utility with the

signature.

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


I want to remove newline character and insert a single whitespace so that

cat $myfile
Using my computer, you can prevent activities related to this info by using fun stuff of this utility with the signature.
-------------------------------------------------------------

sed 's/\n/ /g' myfile
-----is not working

How to do this using sed?

pls help...

Last edited by Fond_of_Opensource; 11-20-2006 at 03:44 AM.
 
Old 11-20-2006, 04:57 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,165
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Code:
sed '/^$/d' myfile
should work

Regards
 
Old 11-20-2006, 05:15 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by bathory
Code:
sed '/^$/d' myfile
should work

Regards
I believe the O.P. wants to slurp all lines up one the first one, but this just removes blank lines. You can easily do it with perl. sed can probably do it too, but there's nothing wrong with a little variety eh?
Code:
perl -ne 'chomp; print "$_ ";' myfile
This might cause you to wind up with some extra whitespace if you have blank lines or spaces at the start or end of lines. This will solve that problem:
Code:
perl -ne 'chomp; s/(^\s+|\s+$)//g; print "$_ " if ( $_ ne "" );' doc
 
Old 11-21-2006, 02:57 AM   #4
Fond_of_Opensource
Member
 
Registered: May 2006
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks matthewg42,

It solved my problem....
 
Old 03-26-2010, 02:19 PM   #5
liuxy
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
Wink More Perl

Quote:
Originally Posted by matthewg42 View Post
I believe the O.P. wants to slurp all lines up one the first one, but this just removes blank lines. You can easily do it with perl. sed can probably do it too, but there's nothing wrong with a little variety eh?
Code:
perl -ne 'chomp; print "$_ ";' myfile
This might cause you to wind up with some extra whitespace if you have blank lines or spaces at the start or end of lines. This will solve that problem:
Code:
perl -ne 'chomp; s/(^\s+|\s+$)//g; print "$_ " if ( $_ ne "" );' doc
Realizing this a old thread but could be useful for someone to google the same question

In Perl you can do:
perl -pi.bk -e 's/^\s+$//' myfile

For brave souls you can skip the .bk part, but don't say I didn't warn you ;-)
 
  


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
help removing some text from a file (sed) BrianK Programming 2 08-13-2006 07:10 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Removing <CR> using sed stv_t Programming 2 10-04-2003 04:10 AM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 05:13 AM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 05:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:55 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