replacing cr/lf with ^M

Caporegime
Joined
18 Oct 2002
Posts
29,493
Location
Back in East London
(That's the control char "^M" not circumflex then 'M'.. (i.e. ctrl+v,ctrl+m).)


I have a file used on a system that I am updating with sed, then patch. I use sed to strip the ^M chars out and replace with \n so that patch can work with it, but I need to change the file back to using ^M instead of \n.. I'm at a loss and every result found on google hasn't helped/worked.

Anyone got any ideas?
 
I think im correct in saying sed wont work in this case because it reads the file line by line, hence consuming the line feeds. I think awk does this as well so they wont be too useful to you.

Do you have perl installed? Something like this should work:

cat myfile | perl -pe 'tr /\xa/\xd/' > newfile that will replace all 0x0A (LF) with 0x0D (^M), I assume thats what your want? Replacing \n with ^M and not \n with ^M+\n (in which case tr wont work), maybe you can do it with the unix tr program, but i remember i couldnt get it working when i was doing something similar once, but perl did the job :)
 
Last edited:
If you have Windows ( or WINE :p ) , there's Notepad++

Format ---> Convert to [ Windows ] [ Unix ] [ Mac ] Format
 
If you have Windows ( or WINE :p ) , there's Notepad++

Format ---> Convert to [ Windows ] [ Unix ] [ Mac ] Format

If you're going to adopt that approach most UNIX systems will have "dos2unix" and "unix2dos" to do the necessary transforms :)
 
Used perl in the end :)

Wrote my own dos2unix/unix2dos scripts. Can't use an editor as this will be applied to 100+ files.. that's a lot of opening and converting to do manually :p
 
Back
Top Bottom