Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I remove with sed all CR and LF from text file (join lines)

share|improve this question
tr -d '\n\r' < file
share|improve this answer
the obviously easiest solution – akira Mar 5 '10 at 18:54

You can use \r and \n, depending on your OS.

\r\n works on windows, \n works on most *nixes, but replacing that with a blank string should merge your lines just fine.

share|improve this answer
The question is quite sed specific (tag and title). Learning simple patterns or recipes for sed, helps develop an understanding to handle more complex cases. – mctylr Mar 5 '10 at 21:58
@mctylr; So? Regex is regex, whether you're using it in sed or perl. Just pointing the op at the pattern they want to match will help them a lot more than a copy&paste command, imo. – Phoshi Mar 5 '10 at 22:10
See page 23 of Mastering Regular Expressions or "Different Regular Expression Engines" in or – mctylr Mar 5 '10 at 23:36
@mctylr; I of course realise the differences in implementation, however matching a single, known character doesn't change between implementations. \r\n is \r\n regardless of who's asking, this is not something under the control of how the engine is implemented. – Phoshi Mar 6 '10 at 0:09

I agree with Dennis Williamson ... here is another alternative:

perl -0pe 's/[\r\n]//g' < in > out

Why do so many questions include "with sed"? I do use sed, but I don't fight its limitations.

share|improve this answer

On an Unix/Linux system I believe this will work for you. I tested it with GNU sed, but I don't think this is using GNU extensions.

sed -e :a -e '/$/N; s/\r\n//; ta;' <in.txt >out.txt
share|improve this answer
If you change it so the cr and nl are in square brackets then it will work even if one or the other is missing or they are in the opposite order: [\r\n] – Dennis Williamson Mar 5 '10 at 19:26
Yes, so it depends if one cares to remove all \r and \n characters, or only \r\n "pairs". Different cases may have different answers. But very good point, thanks. – mctylr Mar 5 '10 at 21:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .