Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to delete/convert conrol character (LF,CR) from text file ?

Posted on 2009-04-04
Medium Priority
Last Modified: 2012-05-06
Windows and DOS uses a pair of CR and LF characters to terminate lines. UNIX (Including Linux and FreeBSD) uses an LF character only. The Apple Macintosh  uses a CR character only.

can some one let me know how to convert text file and delete the control character form text file?
I got to know only how to convert windows text file in unix, and that is perl -i.bak -pe "s/\r//g" file as shared by ozo in my previous post.

Can some one give me perl one-liner to do all the appropriate conversion in text file between hardware platform?
Question by:beer9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

blissbear earned 1000 total points
ID: 24070210
dos to unix:
perl -i.bak -pe "s/\r//g"

unix to dos:
perl -i.bak -pe "s/\n/\r\n/g"

mac to unix:
perl -i.bak -pe "s/\r/\n/g"

unix to mac:
perl -i.bak -pe "s/\n/\r/g"

mac to dos:
perl -i.bak -pe "s/\n/\r\n/g"

dos to mac:
perl -i.bak -pe "s/\n//g"

Expert Comment

ID: 24070214
the regex expression s/x/y/ will replace the x string with y.

Expert Comment

ID: 24070219
on a side note be careful you know what format you are converting from.

If you run dos->mac and then dos->unix on the same data stream you are going to end up stripping all the CR and LF characters.
LVL 40

Expert Comment

ID: 24071474
Linux has a command just for this:

dos2unix oldfile.txt > newfile.txt


Author Closing Comment

ID: 31566702
Thanks! :)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question