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
|
Just after I posted I found this
Code:
sed -n 'H;${g;s/\n/ /g;p}' |
slightly laterally
xargs < file |
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. :)
|
This will remove all newlines:
Code:
cat file | tr -d '\n' Code:
cat file | tr '\n' '\0' | filtering | tr '\0' '\n' |
I never said it was perfect!
Ok so it's a rubbish solution except for simple examples ;) |
I seem to recall using
Code:
sed -ni 's/\n/ /g' Joel |
For every sed one liner, there is a perl one:
Code:
perl -pe 'chomp;' input_file > output_file |
Quote:
Why to have the cat ? Isnt that UUOC |
Quote:
Code:
tr -d '\n' < file |
Quote:
|
that's
Code:
Useless Use Of Cat - UUOC |
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".
|
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 |
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. |