Remove carriage return in Unix

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

提交回复
热议问题