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

Posted on 2009-04-04
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
  • 3

Accepted Solution

blissbear earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now