Yar*_*rin 128 windows macos formatting newline utility
我需要一个转换实用程序/脚本,它将Mac上生成的.sql转储文件转换为Windows上可读的转储文件.这是我在这里遇到的问题的延续.问题似乎是文本文件中的换行格式,但我找不到一个工具来进行转换...
Ann*_*nne 130
Windows使用carriage return+ line feed换行:
\r\n
Run Code Online (Sandbox Code Playgroud)
Unix仅Line feed用于换行:
\n
Run Code Online (Sandbox Code Playgroud)
总之,只需更换每个出现\n的\r\n.
两者unix2dos并dos2unix没有通过在Mac OSX上可用的默认值.
幸运的是,您可以简单地使用Perl或sed完成工作:
sed -e 's/$/\r/' inputfile > outputfile # UNIX to DOS (adding CRs)
sed -e 's/\r$//' inputfile > outputfile # DOS to UNIX (removing CRs)
perl -pe 's/\r\n|\n|\r/\r\n/g' inputfile > outputfile # Convert to DOS
perl -pe 's/\r\n|\n|\r/\n/g' inputfile > outputfile # Convert to UNIX
perl -pe 's/\r\n|\n|\r/\r/g' inputfile > outputfile # Convert to old Mac
Run Code Online (Sandbox Code Playgroud)
代码片段来自:http:
//en.wikipedia.org/wiki/Newline#Conversion_utilities
Jos*_*phH 124
这是Anne的答案的改进版本 - 如果您使用perl,您可以"就地"对文件进行编辑,而不是生成新文件:
perl -pi -e 's/\r\n|\n|\r/\r\n/g' file-to-convert # Convert to DOS
perl -pi -e 's/\r\n|\n|\r/\n/g' file-to-convert # Convert to UNIX
Run Code Online (Sandbox Code Playgroud)
Ste*_*ton 104
您可以使用Homebrew安装unix2dos
brew install unix2dos
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
unix2dos file-to-convert
Run Code Online (Sandbox Code Playgroud)
您还可以将dos文件转换为unix:
dos2unix file-to-convert
Run Code Online (Sandbox Code Playgroud)
Pau*_*l R 16
你可能想要unix2dos:
$ man unix2dos
NAME
dos2unix - DOS/MAC to UNIX and vice versa text file format converter
SYNOPSIS
dos2unix [options] [-c CONVMODE] [-o FILE ...] [-n INFILE OUTFILE ...]
unix2dos [options] [-c CONVMODE] [-o FILE ...] [-n INFILE OUTFILE ...]
DESCRIPTION
The Dos2unix package includes utilities "dos2unix" and "unix2dos" to convert plain text files in DOS or MAC format to UNIX format and vice versa. Binary files and non-
regular files, such as soft links, are automatically skipped, unless conversion is forced.
Dos2unix has a few conversion modes similar to dos2unix under SunOS/Solaris.
In DOS/Windows text files line endings exist out of a combination of two characters: a Carriage Return (CR) followed by a Line Feed (LF). In Unix text files line
endings exists out of a single Newline character which is equal to a DOS Line Feed (LF) character. In Mac text files, prior to Mac OS X, line endings exist out of a
single Carriage Return character. Mac OS X is Unix based and has the same line endings as Unix.
Run Code Online (Sandbox Code Playgroud)
您可以unix2dos使用cygwin在DOS/Windows计算机上运行,也可以使用MacPorts在Mac上运行.
小智 14
只需tr删除:
tr -d "\r" <infile.txt >outfile.txt
Run Code Online (Sandbox Code Playgroud)
vim还可以将文件从 UNIX 格式转换为 DOS 格式。例如:
vim hello.txt <<EOF
:set fileformat=dos
:wq
EOF
Run Code Online (Sandbox Code Playgroud)
相反,如果您需要从 DOS 转到 UNIX:
vim hello.txt <<EOF
:set fileformat=unix
:wq
EOF
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
163419 次 |
| 最近记录: |