如何以编程方式(不使用vi)将DOS/Windows换行符转换为Unix换行符?
dos2unix和unix2dos命令在某些系统上不可用。 如何使用sed、awk和tr等命令模拟它们?
如何以编程方式(不使用vi)将DOS/Windows换行符转换为Unix换行符?
dos2unix和unix2dos命令在某些系统上不可用。 如何使用sed、awk和tr等命令模拟它们?
当前回答
sed -i.bak --expression='s/\r\n/\n/g' <file_path>
因为问题中提到了sed,所以这是使用sed实现此目的的最直接的方法。该表达式表示仅用换行符替换所有换行符和换行符。这就是你从Windows到Unix所需要的。我验证过了。
其他回答
你可以使用tr从DOS转换到Unix;但是,只有当CR仅作为CRLF字节对的第一个字节出现在文件中时,才能安全地执行此操作。通常情况都是这样。然后使用:
tr -d '\015' <DOS-file >UNIX-file
注意DOS-file的名称不同于UNIX-file的名称;如果您尝试使用相同的名称两次,您将在文件中没有数据。
你不能反过来做(用标准的'tr')。
如果你知道如何在脚本中输入回车(control-V, control-M输入control-M),那么:
sed 's/^M$//' # DOS to Unix
sed 's/$/^M/' # Unix to DOS
其中'^M'是control-M字符。你也可以使用bash ANSI-C引用机制来指定回车:
sed $'s/\r$//' # DOS to Unix
sed $'s/$/\r/' # Unix to DOS
但是,如果您必须经常这样做(粗略地说,不止一次),那么安装转换程序(例如dos2unix和unix2dos,或者dou和utod)并使用它们会更明智。
如果你需要处理整个目录和子目录,你可以使用zip:
zip -r -ll zipfile.zip somedir/
unzip zipfile.zip
这将创建一个zip存档,其中行结束符从CRLF更改为cr。unzip将然后将转换后的文件放回原位(并逐个文件询问您-您可以回答:yes to all)。感谢@vmsnomad指出这一点。
有趣的是,在Windows上的Git Bash中,sed ""已经做到了:
$ echo -e "abc\r" >tst.txt
$ file tst.txt
tst.txt: ASCII text, with CRLF line terminators
$ sed -i "" tst.txt
$ file tst.txt
tst.txt: ASCII text
我的猜测是,sed在从输入读取行时忽略它们,并且总是将Unix行结束符写入输出。
安装dos2unix,然后就地转换文件
dos2unix <filename>
将转换后的文本输出到不同的文件使用
dos2unix -n <input-file> <output-file>
你可以在Ubuntu或Debian上安装它
sudo apt install dos2unix
或者在macOS上使用Homebrew
brew install dos2unix
对于Mac OS X,如果您安装了Homebrew (http://brew.sh/):)
brew install dos2unix
for csv in *.csv; do dos2unix -c mac ${csv}; done;
确保您已经复制了文件,因为这个命令将在适当的位置修改文件。 -c mac选项使开关与OS X兼容。
The solutions posted so far only deal with part of the problem, converting DOS/Windows' CRLF into Unix's LF; the part they're missing is that DOS use CRLF as a line separator, while Unix uses LF as a line terminator. The difference is that a DOS file (usually) won't have anything after the last line in the file, while Unix will. To do the conversion properly, you need to add that final LF (unless the file is zero-length, i.e. has no lines in it at all). My favorite incantation for this (with a little added logic to handle Mac-style CR-separated files, and not molest files that're already in unix format) is a bit of perl:
perl -pe 'if ( s/\r\n?/\n/g ) { $f=1 }; if ( $f || ! $m ) { s/([^\n])\z/$1\n/ }; $m=1' PCfile.txt
注意,这将把文件的统一版本发送到标准输出。如果你想用一个统一的版本替换这个文件,添加perl的-i标志。