Welcome Anonymousregister
Categories
· Home
· Authors and Articles
· Downloads
· Feedback
· Forums
· Journal
· Private Messages
· Recommend Us
· Stories Archive
· Submit News
· Surveys
· Top 10
· Topics
· Web Links
· Your Account

Survey
What Topics Of FreeBSD Servers & Workstations Do You Want More Information About?

FreeBSD Installation
Webserver Setup
E-Mail Server Setup
IRC Server Setup
Mailing Lists
Autoresponders
Scripting
Software Coding
Server Security
Installing Ports
Domain Servers
Website Design



Results
Polls

Votes: 23
Comments: 0

 Title : Scripting: How To Batch Remove ^M From PC Files
Scripting



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.



Posted on Sunday, December 02, 2007 @ 17:38:07 EST by Michael

How To Batch Remove ^M From PC Files | Login/Create an Account | 0 comments
The comments are owned by the poster. We aren't responsible for their content.

No Comments Allowed for Anonymous, please register

 
Related Links
· More about Scripting
· News by Michael


Most read story about Scripting:
How To Batch Remove ^M From PC Files


Article Rating
Average Score: 5
Votes: 1


Please take a second and vote for this article:

Excellent
Very Good
Good
Regular
Bad


Options

 Printer Friendly Printer Friendly



Associated Topics

Scripting

1: Credit Repair Combat
128 times
2: My Internet Business
123 times
0.820 Seconds