What is the simplest way to remove all the carriage returns \\r
from a file in Unix?
\r
on any UNIX® system:Most existing solutions in this question are GNU-specific, and wouldn't work on OS X or BSD; the solutions below should work on many more UNIX systems, and in any shell, from tcsh
to sh
, yet still work even on GNU/Linux, too.
Tested on OS X, OpenBSD and NetBSD in tcsh, and on Debian GNU/Linux in bash.
sed
:In tcsh
on an OS X, the following sed snippet could be used together with printf, as neither sed
nor echo handle \r
in the special way like the GNU does:
sed `printf 's/\r$//g'` input > output
tr
:Another option is tr:
tr -d '\r' < input > output
sed
and tr
:It would appear that tr
preserves a lack of a trailing newline from the input file, whereas sed
on OS X and NetBSD (but not on OpenBSD or GNU/Linux) inserts a trailing newline at the very end of the file even if the input is missing any trailing \r
or \n
at the very end of the file.
Here's some sample testing that could be used to ensure this works on your system, using printf
and hexdump -C; alternatively, od -c could also be used if your system is missing hexdump
:
% printf 'a\r\nb\r\nc' | hexdump -C
00000000 61 0d 0a 62 0d 0a 63 |a..b..c|
00000007
% printf 'a\r\nb\r\nc' | ( sed `printf 's/\r$//g'` /dev/stdin > /dev/stdout ) | hexdump -C
00000000 61 0a 62 0a 63 0a |a.b.c.|
00000006
% printf 'a\r\nb\r\nc' | ( tr -d '\r' < /dev/stdin > /dev/stdout ) | hexdump -C
00000000 61 0a 62 0a 63 |a.b.c|
00000005
%
There's a utility called dos2unix that exists on many systems, and can be easily installed on most.
For UNIX... I've noticed dos2unix removed Unicode headers form my UTF-8 file. Under git bash (Windows), the following script seems to work nicely. It uses sed. Note it only removes carriage-returns at the ends of lines, and preserves Unicode headers.
#!/bin/bash
inOutFile="$1"
backupFile="${inOutFile}~"
mv --verbose "$inOutFile" "$backupFile"
sed -e 's/\015$//g' <"$backupFile" >"$inOutFile"
Old School:
tr -d '\r' < filewithcarriagereturns > filewithoutcarriagereturns
I've used python for it, here my code;
end1='/home/.../file1.txt'
end2='/home/.../file2.txt'
with open(end1, "rb") as inf:
with open(end2, "w") as fixed:
for line in inf:
line = line.replace("\n", "")
line = line.replace("\r", "")
fixed.write(line)
Using sed
sed $'s/\r//' infile > outfile
Using sed
on Git Bash for Windows
sed '' infile > outfile
The first version uses ANSI-C quoting and may require escaping \
if the command runs from a script. The second version exploits the fact that sed
reads the input file line by line by removing \r
and \n
characters. When writing lines to the output file, however, it only appends a \n
character. A more general and cross-platform solution can be devised by simply modifying IFS
IFS=$'\r\n' # or IFS+=$'\r' if the lines do not contain whitespace
printf "%s\n" $(cat infile) > outfile
IFS=$' \t\n' # not necessary if IFS+=$'\r' is used
Warning: This solution performs filename expansion (*
, ?
, [...]
and more if extglob
is set). Use it only if you are sure that the file does not contain special characters or you want the expansion.
Warning: None of the solutions can handle \
in the input file.