LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   removing new line character using sed (https://www.linuxquestions.org/questions/linux-newbie-8/removing-new-line-character-using-sed-503201/)

Fond_of_Opensource 11-20-2006 03:29 AM

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...

bathory 11-20-2006 04:57 AM

Code:

sed '/^$/d' myfile
should work

Regards

matthewg42 11-20-2006 05:15 AM

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

Fond_of_Opensource 11-21-2006 02:57 AM

Thanks matthewg42,

It solved my problem.... :)

liuxy 03-26-2010 02:19 PM

More Perl
 
Quote:

Originally Posted by matthewg42 (Post 2510705)
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 ;-)


All times are GMT -5. The time now is 02:35 AM.