Linux下去掉^M的方法

来源:互联网 发布:成都文殊院网络素食 编辑:程序博客网 时间:2024/06/16 18:24


原文地址:http://blog.csdn.net/shouldnotappearcalm/article/details/64904253

昨天在登陆云服务器时候报错。

-bash: $'\r': command not found-bash: $'\r': command not found-bash: $'\r': command not found-bash: /etc/profile: line 11: syntax error near unexpected token `$'{\r'''bash: /etc/profile: line 11: `pathmunge () {
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

后面发现是自己傻逼的把profile在windows上写了传上去的。 
用/bin/cat -v /etc/profile看到很多^M,是因为Windows下的换行符的原因。

现在在linux下面删了就行,我用的第一种方法。

第一种方法(先yum install -y dos2unix): 
cat -A filename 就可以看到windows下的断元字符 ^M 
要去除他,最简单用下面的命令:

dos2unix filename
  • 1

第二种方法:

sed -i 's/^M//g' filename
  • 1

注意:^M的输入方式是 Ctrl + v ,然后Ctrl + M

第三种方法:

vi filename

:1,$ s/^M//g

^M 输入方法: ctrl+V ,ctrl+M

第四种方法: 
cat filename |tr -d ‘/r’ > newfile 
^M 可用 /r 代替

原创粉丝点击