Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

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 oreilly.com/catalog/9780596528126/preview or "Different Regular Expression Engines" in regular-expressions.info/tutorial.html or php.net/manual/en/reference.pcre.pattern.differences.php –  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

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
1  
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
tr -d '\n\r' < file
share|improve this answer
    
the obviously easiest solution –  akira Mar 5 '10 at 18:54

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.