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
Comment Utility
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

Comment Utility
the regex expression s/x/y/ will replace the x string with y.

Expert Comment

Comment Utility
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

Comment Utility
Linux has a command just for this:

dos2unix oldfile.txt > newfile.txt


Author Closing Comment

Comment Utility
Thanks! :)

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

10 Experts available now in Live!

Get 1:1 Help Now