Articles:   Scripting: How To Batch Remove ^M From PC Files (Michael)
  Scripting: Using Date Function To Automate Saving Of Mysql Database Files (Michael)
| Most recent article: How To Batch Remove ^M From PC Files by: Michael 2007-12-02 18:38:07 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.
<------Begin Type Within a Shell------->
#cd /usr/ports/converters/unix2dos
#make clean
===> Cleaning for unix2dos-1.3
#make install distclean
######Code Compiles#######
#rehash
<-------End Type Within a Shell-------->
Once you have compiled unix2dos you will issue the following command from the shell:
<-------Begin Type Within a Shell------->
#find /path/to/folder/of/files/to/process/ -name "*.php" -print -type f -exec dos2unix {} \;
<-------End Type Within a Shell--------->
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:
<-------Begin Type Within a Shell------>
#perl -pi -e 's/\015//g' filename
<-------End Type Within a Shell-------->
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. read more... |