Tuesday, March 21, 2006

Removing carriage returns (^M) from a file

When transferring a file from UNIX to Windows in binary mode, you may observe carriage returns (control M characters) in the file when viewing it on UNIX. The control M character(^M) is the end of line character on Windows . When you transfer afile via FTP in binary mode, the file is transferred with the carriage returns intact. The control M character is not the end of line character in UNIX, so as a result the ^M is interpreted as part of the file. In order to avoid this problem the file should be transferred in ASCII mode. If this is not possible there are scripts which can remove the Control M character. Below is an example of using the tr command to remove the control M characters from a file:

tr -d "\015" < $input > $output

The tr command reads in characters from standard input and replaces or deletes characters before writing them back to standard output. The -d option in the example above will delete the carriage returns (specified in octal as o15) from the specified input file ($input) and redirect the results to an output file ($output). Here is in a for loop:

for f in `ls `

do

tr -d "\015" <$f >tmp/$f

done




Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home