Remove carriage return in Unix

后端 未结 21 2217
傲寒
傲寒 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:54

    Someone else recommend dos2unix and I strongly recommend it as well. I'm just providing more details.

    If installed, jump to the next step. If not already installed, I would recommend installing it via yum like:

    yum install dos2unix
    

    Then you can use it like:

    dos2unix fileIWantToRemoveWindowsReturnsFrom.txt
    
    0 讨论(0)
  • 2020-11-22 03:55

    The simplest way on Linux is, in my humble opinion,

    sed -i 's/\r$//g' <filename>
    

    The strong quotes around the substitution operator 's/\r//' are essential. Without them the shell will interpret \r as an escape+r and reduce it to a plain r, and remove all lower case r. That's why the answer given above in 2009 by Rob doesn't work.

    And adding the /g modifier ensures that even multiple \r will be removed, and not only the first one.

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

    I made this shell-script to remove the \r character. It works in solaris and red-hat:

    #!/bin/ksh
    
    LOCALPATH=/Any_PATH
    
    for File in `ls ${LOCALPATH}`
    do
       ARCACT=${LOCALPATH}/${File}
       od -bc ${ARCACT}|sed -n 'p;n'|sed 's/015/012/g'|awk '{$1=""; print $0}'|sed 's/ /\\/g'|awk '{printf $0;}'>${ARCACT}.TMP
       printf "`cat ${ARCACT}.TMP`"|sed '/^$/d'>${ARCACT}
       rm ${ARCACT}.TMP
    done
    
    exit 0
    
    0 讨论(0)
  • 2020-11-22 03:57
    tr -d '\r' < infile > outfile
    

    See tr(1)

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

    If you're using an OS (like OS X) that doesn't have the dos2unix command but does have a Python interpreter (version 2.5+), this command is equivalent to the dos2unix command:

    python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('\r', '\n') for line in fileinput.input(mode='rU'))"
    

    This handles both named files on the command line as well as pipes and redirects, just like dos2unix. If you add this line to your ~/.bashrc file (or equivalent profile file for other shells):

    alias dos2unix="python -c \"import sys; import fileinput; sys.stdout.writelines(line.replace('\r', '\n') for line in fileinput.input(mode='rU'))\""
    

    ... the next time you log in (or run source ~/.bashrc in the current session) you will be able to use the dos2unix name on the command line in the same manner as in the other examples.

    0 讨论(0)
  • 2020-11-22 04:01

    I'm going to assume you mean carriage returns (CR, "\r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:

    $ cat infile
    hello
    goodbye
    
    $ cat infile | od -c
    0000000   h   e   l   l   o  \r  \n   g   o   o   d   b   y   e  \n
    0000017
    

    dos2unix is the way to go if it's installed on your system:

    $ cat infile | dos2unix -U | od -c
    0000000   h   e   l   l   o  \n   g   o   o   d   b   y   e  \n
    0000016
    

    If for some reason dos2unix is not available to you, then sed will do it:

    $ cat infile | sed 's/\r$//' | od -c
    0000000   h   e   l   l   o  \n   g   o   o   d   b   y   e  \n
    0000016
    

    If for some reason sed is not available to you, then ed will do it, in a complicated way:

    $ echo ',s/\r\n/\n/
    > w !cat
    > Q' | ed infile 2>/dev/null | od -c
    0000000   h   e   l   l   o  \n   g   o   o   d   b   y   e  \n
    0000016
    

    If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)

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