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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 178
Perl, group, sort, count question 6 141
Perl script to parse xml file 2 125
Rename file based on contents in the file? 3 97
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

860 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