Welcome Anonymousregister

FreeBSD Hacks: Forums


FreeBSD Hacks :: View topic - Learn How-To Remove ^M Carriage Returns From PC Files
main forum search user groups members list profile pm faq
Learn How-To Remove ^M Carriage Returns From PC Files
Post new topic   Reply to topic View previous topic :: View next topic
Author Message
Michael

Site Admin


Joined: Nov 21, 2007
Posts: 4

PostPosted: Sun Dec 02, 2007 8:49 pm    Post subject: Learn How-To Remove ^M Carriage Returns From PC Files Reply with quote
 
Removing ^M From Files Saved Within Windows Is Easy Using This Utility.

This is probably one of the most annoying issues you have to deal with when you open a lot of files which have either been on a windows system or were created on a windows machine, and that is removing those pesky ^M symbols when viewed on a Unix box. Generally these carriage return symbols (^M) don't do any harm when they are within Unix files but they are annoying and make the file your viewing that much harder to read.

If you are anything like I am when it comes to these, you want an easy way to remove them from a batch of files all at once and not waste a whole lot of time on them. The utility I use to remove them is called dos2unix and it can be found in the ports tree at /usr/ports/converters/unix2dos Now you're probably asking, 'Wait, I thought you said it was dos2unix', yes you're right I did, but included in the unix2dos application is the dos2unix command which we will be using.

Code:

#cd /usr/ports/converters/unix2dos
#make clean
===>  Cleaning for unix2dos-1.3
#make install distclean
######Code Compiles#######
#rehash


Once you have compiled unix2dos you will issue the following command from the shell:

Code:

#find /path/to/folder/of/files/to/process/ -name "*.php" -print -type f -exec dos2unix {} \;


This will spit out a list of the files that it found within this folder and execute the dos2unix command on the files with the extension of .php.

Now there are probably a million different ways to accomplish the task of removing these carriage return within your files, but I found that using this method to do a batch search and execute is the simplest most effective way of doing this chore.

You can use other tools to remove these ^M symbols from your files and if you're are just dealing with one file then this may be a little overboard. You could use perl to do this for example by doing the following:

Code:

#perl -pi -e 's/\015//g' filename


I hope you find this as useful as I do when dealing with files that you move from a windows box to your FreeBSD machine. These are the kinds of hacks that make using FreeBSD so much fun.

_________________
Michael
Director of Business Development
webmaster (AT) freebsdhacks (DOT) com
Back to top

View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001-2004 phpBB Group

MetalTech commercial theme pack by MDesign mtechnik.net
Forums ©
1: Credit Repair Combat
128 times
2: My Internet Business
123 times
0.262 Seconds