|
|
Friday, November 28, 2008
Converting ^M
I keep running into files that have weird encoding for returns. I suspect it is a combination of optimization and obfuscation. I particularly see this with javascript files.
Here is the fix…
First, to get the ^M which is a control M, type ctrl v followed by ctrl m.
To get ^@ type ctrl v followed by ctrl shift 2 (or ctrl v followed by
Second, the conversion…
In vi (or vim) use the following:
:%s/^M/\n/g
or with perl on the command line:
$ perl -pi.bak -e ’s/^M/\n/g’
^M is ASCII 13 (Ctrl M), which is the carriage return.
Different operating systems use different symbols to set the end of a line/new line.
Unix uses newline (\n)
Mac uses carriage return (\r)
And Windows/DOS use both (\n\r)
The reason I put the ^@ one on there is because some times when I convert the ^M to \n, it becomes ^@ instead, which doesn’t help. Well, in reality it does. You just convert the ^@ back to ^M. When you convert the ^@ back to ^M, then the code looks all pretty.
In vi (or vim) use the following:
:%s/^@/^M/g
or with perl on the command line:
$ perl -pi.bak -e ’s/^@/^M/g’
As with all things nerdy… your mileage may vary.
Comments:
Being involved with web application development as well, I have to say, CTRL-M’s suck.
There is also a nice utility called dos2unix (and unix2dos) that can be installed via the Debian package ‘tofrodos’.
Another favorite Debian package of mine, if you use SecureCRT, is ‘lrzsz’. It allows you to do zmodem (remember the old BBS days?) transfers directly through your SSH connection. Just type ‘rz’ at the command line and SecureCRT will pop up a dialog box asking what file you want to upload.
-g
Geurk,
Thanks for the comment. You are correct in that dos2unix is a great tool. Between that tool and unix2dos and mac2unix you can convert all your \n\r’s to \n’s as needed. However, there are times where this won’t work. I ran in to one such time and, hence, my posting about doing it manually.
As with all things nerdy… your mileage may vary.
--Moose