Tuesday, July 19, 2011

How to remove control characters from text file

tr -d '\015\032' < dosformatfile.txt > unixformatfile.txt
The above command deletes all ^M and ^Z

Using Perl:
perl -i -ep 's/\015//g' filename
perl -pi.bak -0777 -e 's#\r##gi' filename

using sed:
sed -e "s/^M/" filename > newfilename
press ctrl them V & M

Using vi:
vi filename
:s/^M//g
press control then V & M


with:
:%s/^M/\r/g
works perfectly !!!

Heres another little script
#!/bin/sh
FILE="$1"
# Use sed with the -i command line for inline interpreting.
sed -i '' "s/\r//g" $FILE


# the end

Hope this helps. Rupam