如何以编程方式(不使用vi)将DOS/Windows换行符转换为Unix换行符?
dos2unix和unix2dos命令在某些系统上不可用。 如何使用sed、awk和tr等命令模拟它们?
如何以编程方式(不使用vi)将DOS/Windows换行符转换为Unix换行符?
dos2unix和unix2dos命令在某些系统上不可用。 如何使用sed、awk和tr等命令模拟它们?
当前回答
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标志。
其他回答
如果您没有访问dos2unix的权限,但是可以阅读这个页面,那么您可以从这里复制/粘贴dos2unix.py。
#!/usr/bin/env python
"""\
convert dos linefeeds (crlf) to unix (lf)
usage: dos2unix.py <input> <output>
"""
import sys
if len(sys.argv[1:]) != 2:
sys.exit(__doc__)
content = ''
outsize = 0
with open(sys.argv[1], 'rb') as infile:
content = infile.read()
with open(sys.argv[2], 'wb') as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + '\n')
print("Done. Saved %s bytes." % (len(content)-outsize))
(超级用户交叉发布。)
有趣的是,在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行结束符写入输出。
我不得不思考同样的问题(在windows方面,但同样适用于Linux)。
令人惊讶的是,没有人提到一种非常自动化的方法,使用旧的zip -ll选项(Info-ZIP)对文本文件进行CRLF <-> LF转换:
zip -ll textfiles-lf.zip files-with-crlf-eol.*
unzip textfiles-lf.zip
注意:这将创建一个ZIP文件,保留原始文件名,但将行结束符转换为LF。然后unzip将解压压缩后的文件,即使用它们的原始名称(但使用lf结尾),从而提示覆盖本地原始文件(如果有的话)。
zip——help的相关摘录:
zip --help
...
-l convert LF to CR LF (-ll CR LF to LF)
对于Mac OS X,如果您安装了Homebrew (http://brew.sh/):)
brew install dos2unix
for csv in *.csv; do dos2unix -c mac ${csv}; done;
确保您已经复制了文件,因为这个命令将在适当的位置修改文件。 -c mac选项使开关与OS X兼容。
作为Jonathan Leffler的Unix to DOS解决方案的扩展,当你不确定文件当前的行结束符时,可以安全地转换为DOS:
sed '/^M$/! s/$/^M/'
这将在转换为CRLF之前检查该行是否已经在CRLF中结束。