分享交流
合作共赢!

Windows文件dos与Unix格式相互转换方法总结

一、简介

在执行shell脚本时提示这样的错误主要是由于shell脚本文件是dos格式,即每一行结尾以\r\n来标识,而unix格式的文件行尾则以\n来标识。

查看脚本文件是dos格式还是unix格式的几种办法。
(1)cat -A filename  从显示结果可以判断,dos格式的文件行尾为^M$,unix格式的文件行尾为$。
(2)od -t x1 filename 如果看到输出内容中存在0d 0a的字符,那么文件是dos格式,如果只有0a,则是unix格式。
(3)vi filename打开文件,执行 : set ff,如果文件为dos格式在显示为fileformat=dos,如果是unxi则显示为fileformat=unix。

二、解决方法

在Linux下使用vi来查看一些在Windows下创建的文本文件,有时会发现在行尾有一些“^M”。有几种方法可以处理。

注意事项:

  • ^M的输入方式是 Ctrl+Shift + V ,然后Ctrl +Shift+ M 
  •  ^M 可用 /r 代替

方法一:

使用cat命令就可以看到windows下的断元字符 ^M

cat -A filename

要去除他,最简单用下面的命令:

dos2unix filename

方法二:

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

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

方法三:

使用vi的替换功能。启动vi,进入命令模式,输入以下命令

  1. :%s/^M$//g # 去掉行尾的^M。
  2. :%s/^M//g # 去掉所有的^M。
    
    
  3. :%s/^M/[ctrl-v]+[enter]/g # 将^M替换成回车。
    
    
  4. :%s/^M/\r/g # 将^M替换成回车。

#注意:^M 输入方法: ctrl+Shift+V ,ctrl+Shift+M

方法四:

cat filename |tr -d '/r' > newfile

方法五

vi filename打开文件,执行 : set ff=unix 设置文件为unix,然后执行:wq,保存成unix格式。

Other way:

The error:

'\r': command not found

is caused by shell not able to recognise Windows-like CRLF line endings (0d 0a) as it expects only LF (0a).


Git

If you using Git on Windows, make sure you selected ‘Checkout as-is‘ during setup. Then make sure that you run: git config --global core.autocrlf false, so Git will not perform any conversions when checking out or committing text files.


dos2unix

If you’re not using Git, you simply need to convert these affected files/scripts back into Unix-like line endings (LF), either by:

dos2unix ~/.bashrc

Note: The dos2unix command is part of dos2unix package.

Ex/Vim editor + tr

If you’ve Vim installed, the following command should correct the files:

ex +'bufdo! %! tr -d \\r' -scxa ~/.bash*

Useful alias: alias dos2unix="ex +'bufdo! %! tr -d \\\\r' -scxa".

tr

Here is the method by using tr:

cat ~/.bashrc | tr -d '\r' > ~/.bashrc.fixed && mv -v ~/.bashrc.fixed ~/.bashrc

or:

tr -d '\r' < filename > new_filename

Note: The \r is equivalent to \015.


sed

You can try the following command:

sed -i'.bak' s/\r//g ~/.bash*

recode

The following aliases can be useful (which replaces dos2unix command):

alias unix2dos='recode lat1:ibmpc'
alias dos2unix='recode ibmpc:lat1'

Source: Free Unix Tools (ssh, bash, etc) under Windows.


perl

The following perl command can convert the file from DOS into Unix format:

perl -p -i.bak -e 's/\015//g' ~/.bash*

Source: stripping the ^M.


tofrodos

On Linux, like Ubuntu which doesn’t come standard with either dos2unix or unix2dos, you can install tofrodos package (sudo apt-get install tofrodos), and define the following aliases:

alias dos2unix=’fromdos’
alias unix2dos=’todos’

Then use in the same syntax as above.


Vagrant

If you’re using Vagrant VM and this happens for provisioning script, try setting binary option to true:

# Shell provisioner, see: https://www.vagrantup.com/docs/provisioning/shell.html
config.vm.provision "shell" do |s|
  s.binary = true # Replace Windows line endings with Unix line endings.
  s.path = "script.sh"
end

See: Windows CRLF to Unix LF Issues in Vagrant.

赞(0) 打赏
未经允许不得转载:琼杰笔记 » Windows文件dos与Unix格式相互转换方法总结

评论 抢沙发

评论前必须登录!

 

分享交流,合作共赢!

联系我们加入QQ群

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册