Remove carriage return in Unix

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

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

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

    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.

提交回复
热议问题