使用sed在Mac OS X上删除回车 [英] Removing Carriage return on Mac OS X using sed

查看:158
本文介绍了使用sed在Mac OS X上删除回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于删除回车符的Linux上,我们可以执行:

On Linux for removing carriage return we can execute:

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

但是在Mac OS X上却无法使用.需要在$之前加上

But the same will not work on Mac OS X. Need to prepend $ like:

sed -i $'s/\r//' <file>

也不需要"g".

为什么会这样?

推荐答案

这是因为与Linux上的sed不同,OSX上可用的sed不能将\r识别为特殊字符.

It is because sed available on OSX doesn't recognize \r as a special character unlike the sed on Linux does.

您可以按照使用方式使用它:

You can either use it the way you're using:

sed -i.bak $'s/\r//' file

或者这个:

sed -i.bak "s/$(printf '\r')//" file

否则您可以在OSX上使用tr:

OR else you can use tr on OSX:

tr -d '\r' < file

这篇关于使用sed在Mac OS X上删除回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆