LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sed remove trailing newline (https://www.linuxquestions.org/questions/programming-9/sed-remove-trailing-newline-530376/)

nc3b 02-19-2007 06:55 AM

Sed remove trailing newline
 
Hello everyone. I have a text composed of many lines. Please, can you tell me how can I get rid of the newlines and join the text in one big line? Thank you

nc3b 02-19-2007 07:00 AM

Just after I posted I found this
Code:

sed -n 'H;${g;s/\n/ /g;p}'
If you have a better (simpler) way I would be thankful if you would post it

bigearsbilly 02-19-2007 07:03 AM

slightly laterally

xargs < file

nc3b 02-19-2007 09:20 AM

Thank you very much. Your example is (slightly:p) simpler.. But I will decide to use it because it may prove to be.. uh, more portable, yes, that's it :)) Again, thank you. :)

firstfire 02-19-2007 09:22 AM

This will remove all newlines:
Code:

cat file | tr -d '\n'
If you want to find regex matches over whole file with sed, it may be useful to translate newlines to zero-chars '\0'. Then, after filtering, you will be able to translate it back:
Code:

cat file | tr '\n' '\0' | filtering | tr '\0' '\n'
PS: bigearsbilly: Really cool! But I encounter an error when `file' contain unmatched single quote (').

bigearsbilly 02-19-2007 09:51 AM

I never said it was perfect!

Ok so it's a rubbish solution except for simple examples
;)

trashbird1240 02-20-2007 12:11 PM

I seem to recall using

Code:

sed -ni 's/\n/ /g'
See if that works.
Joel

matthewg42 02-20-2007 03:22 PM

For every sed one liner, there is a perl one:

Code:

perl -pe 'chomp;' input_file > output_file

kshkid 02-20-2007 10:12 PM

Quote:

Originally Posted by firstfire
This will remove all newlines:
Code:

cat file | tr -d '\n'
If you want to find regex matches over whole file with sed, it may be useful to translate newlines to zero-chars '\0'. Then, after filtering, you will be able to translate it back:
Code:

cat file | tr '\n' '\0' | filtering | tr '\0' '\n'
PS: bigearsbilly: Really cool! But I encounter an error when `file' contain unmatched single quote (').


Why to have the cat ?

Isnt that UUOC

firstfire 02-20-2007 11:49 PM

Quote:

Originally Posted by kshkid
Why to have the cat ?

Isnt that UUOC

You can do the same without `cat' if you want:

Code:

tr -d '\n' < file
What is UUOC?

////// 02-21-2007 01:47 AM

Quote:

Originally Posted by firstfire
What is UUOC?

I have no idea but, Unnecessary Use Of Cat would match :P

kshkid 02-21-2007 03:18 AM

that's

Code:

Useless Use Of Cat - UUOC
:)

unSpawn 02-21-2007 07:27 AM

While I agree you can do that w/o cat this example really is a minor nit IMHO. A better example of UUOC would be those situations where you "cat file|awk" or "cat file|sed" where you can "awk file" or "sed -i file".

bigearsbilly 02-21-2007 07:42 AM

UUOC indeed, but i would say the first example
is easier to scan and edit:

cat file | sort -k1,2rn | cut -d\ -f3-9 | grep -v INFO

sort -k1,2rn < file | cut -d\ -f3-9 | grep -v INFO



IMnsHO

Nick_Battle 02-21-2007 08:40 AM

Looks like you've got a solution already, but I thought I'd mention the fmt command, which is aimed at text formatting - unfortunately (at least on HP-UX!) it doesn't have an option to fill/join lines to an arbitrary width. But it's occasionally useful to fill lines to a certain width... in particular, in vi typing !}fmt will line-fill the current paragraph, which is very handy when entering a lot of text.

"xargs < file" is the nicest one-liner I've seen in a long time :-)


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