Remove carriage return in Unix

2018-12-31 08:25发布

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

16条回答
人气声优
2楼-- · 2018-12-31 09:04

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"
查看更多
宁负流年不负卿
3楼-- · 2018-12-31 09:05

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
查看更多
还给你的自由
4楼-- · 2018-12-31 09:07

you can simply do this :

$ echo $(cat input) > output
查看更多
唯独是你
5楼-- · 2018-12-31 09:13

Here is the thing,

%0d is the carriage return character. To make it compatabile with Unix. We need to use the below command.

dos2unix fileName.extension fileName.extension

查看更多
登录 后发表回答