Friday, August 18, 2006
Remove Carriage Return from Files
Using AWK
To use awk to convert a Windows file to Unix, at the Unix prompt, enter:
awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt
To convert a Unix file to Windows using awk, at the command line, enter:
awk 'sub("$", "\r")' unixfile.txt > winfile.txt
On some systems, the version of awk may be old and not include the function sub. If so, try the same command, but with gawk or nawk replacing awk.
Using TR
You can use tr to remove all carriage returns and Ctrl-z ( ^Z ) characters from a Windows file by entering:
tr -d '\15\32' < winfile.txt > unixfile.txt
You cannot use tr to convert a document from Unix format to Windows.
Using vi
In vi, you can remove the carriage return ( ^M ) characters with the following command:
:1,$s/^M//g
Note: To input the ^M character, press Ctrl-v , then press Enter or return.
Subscribe to:
Comments (Atom)