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.

Posted by Moose on 11/28 at 01:58 PM
JobLinux and UnixPermalink
Comments:
Geurk on 12/04 at 10:38 AM said:

Being involved with web application development as well, I have to say, CTRL-M’s suck. smile

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


Moose on 12/04 at 11:58 AM said:

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


Page 1 of 1 pages
QUOTE OF THE DAY:
Chance favors the prepared mind - Louis Pasteur
Things I want:
Apparently I am to list some things I want as I never tell anyone and when I do its in the form of "I just got", so here is a list

Roasted and ground
Dirac on quantum mechanics
von Neumann and Morgenstern on game theory
Wiener on cybernetics
Feynman on quantum electrodynamics
Einstein's collected papers (all volumes)
The Art of Computer Programming by Donald E. Knuth. (all volumes)
Oh, and to understand women.

Tools:
translate binary and stuff
What I'm watching:
movie image Little Miss Broadway
movie image The Hurt Locker

Please Visit:

Some of the Web sites I have worked on in my career:
CURRENT:
State of Indiana
PAST:
www.aaronsdreamweeknd.com
www.aaronssuperfan.com
www.acctriviachallenge.com
www.allteltriviachallenge.com
www.arizonaathletics.com
www.baltimorebayhawks.com
www.big12sports.com
www.boomshea.com
www.bostoncannons.com
www.bridgeportbarrage.com
www.cha-health.com
www.chahealthagents.com
www.championship.usyouthsoccer.org
www.CommerceLexington.com
www.crosssphere.com
www.eastwestcollege.com
www.ecupirates.com
www.experienceispa.com
www.finalfour.net
www.futurelab.com
www.getasidekick.com
www.ghsa.net
www.gomarquette.com
www.gwinnettdailyonline
www.gwinnettdailypost.com
www.hoopitup.com
www.horizonleague.org
www.hostcommunications.com
www.iHigh.com
www.ispaconference.com
www.jpsports.com
www.kentstatesports.com
www.lexsports.com
www.lfsports.com
www.lonestarconference.org
www.lonestarshowdown.net
www.longislandlizards.com
www.maacsports.com
www.mackbrown-texasfootball.com
www.madisonbankky.com
www.majorleaguelacrosse.com
www.millenniumfarms.com
www.mgoblue.com
www.mondaymorningmessages.com
www.mstateathletics.com
www.ncaabaseball.com
www.ncaachampionships.com
www.ncaafootball.com
www.ncaafootball.net
www.ncaaicehockey.com
www.ncaalacrosse.com
www.ncaasoccer.net
www.ncaasoftball.com
www.ncaatradeshow.com
www.ncaavolleyball.net
www.ncaawrestling.com
www.newjerseypride.com
www.newtoncitizen.com
www.ntaonline.com
www.orugoldeneagles.com
www.philadelphiabarrage.com
www.polkspearls.com
www.raycomsports.com
www.redroom.com
register.imgacademies.com
www.rochesterrattlers.com
www.rockdalecitizen
www.schoolcounts.com
www.seckids.com
www.secsports.com
www.secsports.org
www.soccer3v3.com
www.southernsportsawards.com
www.sportsmarketing.org
www.statefarmlonestarshowdown.com
www.takg.com
www.teammomusa.com
www.texasfootball.com
www.texassports.com
www.traveltheperfectfreedom.com
www.triplecrownmedia.com
www.troytrojans.com
www.ukathletics.com
www.uksportsmarketing.com
www.uksportsmarketing.org
www.uscowboytour.com
www.usmiles.com
www.usyouthsoccer.org
www.usysregion3.org
www.uterwincenter.com
www.utladyvols.com
www.utsports.com
www.vinerykentucky.com
www.webberathletics.com

 

 

 

 

 

 

 

 

 

 

 

 

RSS Feed