remove ^M characters from file using sed

前端 未结 9 2285
情书的邮戳
情书的邮戳 2020-12-01 15:35

I have this line inside a file:

ULNET-PA,client_sgcib,broker_keplersecurities
,KEPLER

I try to get rid of that ^M (carriage return) charact

相关标签:
9条回答
  • 2020-12-01 16:10

    Use tr:

    tr -d '^M' < inputfile
    

    (Note that the ^M character can be input using Ctrl+VCtrl+M)


    EDIT: As suggested by Glenn Jackman, if you're using bash, you could also say:

    tr -d $'\r' < inputfile
    
    0 讨论(0)
  • 2020-12-01 16:12

    In awk:

    sub(/\r/,"")
    

    If it is in the end of record, sub(/\r/,"",$NF) should suffice. No need to scan the whole record.

    0 讨论(0)
  • 2020-12-01 16:21

    still the same line:

    sed -i 's/^M//g' file
    

    when you type the command, for ^M you type Ctrl+VCtrl+M

    actually if you have already opened the file in vim, you can just in vim do:

    :%s/^M//g
    

    same, ^M you type Ctrl-V Ctrl-M

    0 讨论(0)
  • 2020-12-01 16:23

    You can simply use dos2unix which is available in most Unix/Linux systems. However I found the following sed command to be better as it removed ^M where dos2unix couldn't:

    sed 's/\r//g' < input.txt >  output.txt
    

    Hope that helps.

    Note: ^M is actually carriage return character which is represented in code as \r What dos2unix does is most likely equivalent to:

    sed 's/\r\n/\n/g' < input.txt >  output.txt
    

    It doesn't remove \r when it is not immediately followed by \n and replaces both with just \n. This fails with certain types of files like one I just tested with.

    0 讨论(0)
  • 2020-12-01 16:26

    If Perl is an option:

    perl -i -pe 's/\r\n$/\n/g' file

    -i makes a .bak version of the input file
    \r = carriage return
    \n = linefeed
    $ = end of line
    s/foo/bar/g = globally substitute "foo" with "bar"

    0 讨论(0)
  • 2020-12-01 16:29

    This is clean and simple and it works:

    sed -i 's/\r//g' file
    

    where \r of course is the equivalent for ^M.

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