Tuesday, June 30, 2009

Remove Carriage Returns From Text Files Edited on Windows

Sometimes people change text files - that are supposed to run or get processed on a Unix/Linux system - on Windows text editors such as notepad, this messes up the file as Windows puts a carriage return (read as ^M from within vi) at the end of each line and then you won't be able to execute or process the file correctly, here is the Linux command you can use to remove such invalid characters:

tr -d '\r' "<"httpd.conf ">"httpd_clean.conf (remove the double quotations)

where:

tr -> is the command
-d -> to delete a certain character or pattern of characters
'\r' -> denotes the carriage return (elly heyya el Enter)
httpd.conf is the input file, it has to be preceded by a '<' >httpd_clean.conf -> httpd_clean.conf is the output file, it has to be preceded by a '>'

4 comments:

mhewedy said...

very useful command that i regularly needs. Although i could use notepad++ for such situation, but this command seems a good competitor.

Ahmad Amr said...

I actually don't use Windows at all and so all my files are clean. The problem occurs when Windows people copy the text files from the server to their local windows machines to edit (instead of using vi) most likely using notepad or wordpad and then re-send them back to the server, I then have to run this command to clean it up.

mhewedy said...

I encounter a similar situation as people you talk about.

There is a simple and easy solution for such people; using SCP clients such as WinSCP.

It has a GUI interface, you can double click the file, edit it, click save, and it handles all the story for you.

So, they don't need to copy files to/from Linux servers.

BooDy said...

i think i never used this command before!!

but seems like i'll be using it from now on as its extremely useful :)