HowTo: Convert Between Windows & Unix Line Endings Using PERL

Linux, Mac, Windows No Comments »

You may or may not know that Windows and Unix end lines in text files differently. Windows uses both a line feed and carriage return characters whereas Unix uses only the line feed character. This can be annoying if you’re working with documents on both systems interchangeably and you need to preserve a lot of indentation . One example for me is xml files I like writing them in a plain text editor because I cannot find an xml editor I get on with.

The solution is to use PERL:

  • To convert Unix style line endings to Windows style
    • perl -p -e ’s/\n/\r\n/’ < unixfile.txt > winfile.txt
  • To Convert Windows style line endings to Unix style
    • perl -p -e ’s/\r$//’ < winfile.txt > unixfile.txt

The use of single quotation marks in both command lines is important because it prevents the shell from trying to evaluate anything inside.

That’s it. A simple way to quicky convert line endings letting you get on with the job.

WP Theme & Icons by N.Design Studio | Akismet has gobbled 83,520 spam comments...Mmmm Tasty :-)
Entries RSS Comments RSS Log in