Thursday 3 May 2007

Converting several textfiles between Unix and Windows linebreak using Perl

I have a bunch of text files and I want to move them to Windows (which as usually only follows one standard (it's own) and is unable to properly interpret Unix line breaks (unlike most Unix text-tools that can manage both standards)). If I just move them they will look horrible and my client will complain. So, what to do. Time to deploy a friendly Perl hacker (fadern), which I happen to know. Tinker a bit and soon you will have a one-liner that solves your problem.

Unix -> Windows
> perl -pi'.bak' -e 's/\012/\015\012/g' *.suffix

Windows -> Unix
> perl -pi'.bak' -e 's/\015\012/\012/g' *.suffix

These commands will create backups of the changed files with a .bak suffix. If you feel that you don't need that just remove '.bak' from the command. Just remember that if you screw up it's permanent.

1 comment:

Anonymous said...

w00t
Now I know Unix is an OS and Perl a programming language.
And understand more the importance (and nuisance) of line breaks in text-based programming.

Or well, not really, but lightly browsing wikipedia makes my brain feel all shiny