Remove carriage return in Unix

后端 未结 21 2174
傲寒
傲寒 2020-11-22 03:40

What is the simplest way to remove all the carriage returns \\r from a file in Unix?

相关标签:
21条回答
  • 2020-11-22 03:48

    Removing \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.


    With 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
    

    With tr:

    Another option is tr:

    tr -d '\r' < input > output
    

    Difference between 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.


    Testing:

    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
    % 
    
    0 讨论(0)
  • 2020-11-22 03:50

    There's a utility called dos2unix that exists on many systems, and can be easily installed on most.

    0 讨论(0)
  • 2020-11-22 03:51

    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"
    
    0 讨论(0)
  • 2020-11-22 03:52

    Old School:

    tr -d '\r' < filewithcarriagereturns > filewithoutcarriagereturns
    
    0 讨论(0)
  • 2020-11-22 03:53

    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)
    
    0 讨论(0)
  • 2020-11-22 03:53

    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.

    0 讨论(0)
提交回复
热议问题