Monday, February 23, 2009

cr/lf linebreak madness

I get these log files in my work that are supposed to be text files. Supposed to be. For some bizarre reason the CRs are there (represented by ^M) while the linefeed character seems to be missing. So doing this:

sed 's/^M//g' < inputfile.txt > outputfile.txt


does NOT work.

After much searching and experimenting, this works:

perl -ne ' s/:/\n/g; print ' inputfile.txt > outputfile.txt


I have yet to find a sed version that works. Stay tuned.

PS. Check out the wikipedia entry on this for other ideas: http://en.wikipedia.org/wiki/Newline

1 comment:

raoul said...

Looking at this again, I suspect the proper solution be something like:

sed 's/^M/\r/g < inputfile.txt > outputfile.txt

or whatever the code is for carriage return. I think it should be \r and not \n for Nix type OSes.