Thursday, October 16, 2008

unix and windows: carriage returns with tr/sed/awk

cat -v myfile.csv

...and then you will see them at the end of the line as ^M (Ctrl-M)

delete them using tr like this, but this will only work in the following form if "\r" is recognised as a carriage return character. Some versions of tr and sed do not. So before you try this out you should set up a test file with a few "r"s in it in obvious places to make sure the following command is not just deleting "r"s.

tr -d '\r' <> outfile.csv

If the above command deleted "r"s instead of carriage returns then instead of using '\r' use 'Cntl-v-m'. The Cntl-v works on a few shells to let it know that you are going to enter a special character and the "m" you follow it with indicates a carriage return. "m" as in the "^M" you see at the end of the lines when you use cat -v.

There are other ways of removing these carriage returns. You can use sed to do it but again you have to check is it accepts "\r" as a carriage return by experimenting on a very small test file with a few obvious "r"s in it.

sed 's/\r$//' infile.csv > outfile.csv

On a final note, you might have to convert a Unix file to a DOS/Windows file sometimes. You can do it like this:

awk '{ print $0 "\r"}' unix.txt > dos.txt

---

simple:
tr -d '\015' <> new.file

more simple:
perl -pi -e 's/\015//g' file

No comments:

Post a Comment