将换行格式从Mac转换为Windows

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.
两者unix2dosdos2unix没有通过在Mac OSX上可用的默认值.
幸运的是,您可以简单地使用Perlsed完成工作:

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

  • UNIX到DOS的`sed`命令在OS X Lion上对我不起作用 - 它只是在每行的末尾插入文本"r".但是`perl`命令有效. (34认同)
  • OS X Yosemite仍然和`sed`有同样的问题,但你可以解决它而无需安装Homebrew,gnu-sed或unix2dos:使用`sed -e'/ $/^ M /'inputfile> outputfile`,其中` ^ M`是通过`Ctrl + V Ctrl + M`在命令行上生成的控制字符. (9认同)
  • OSX使用旧版本的sed.我使用Homebrew for OSX,并安装了gnu-sed.您使用"gsed"命令而不是"sed".这样可行. (6认同)
  • Mac OS 的另一种解决方法(在 10.13.6 High Sierra 上测试):在包含 sed 命令的单引号之前放置一个 `$`:`sed $'s/\r$//'` 说明:bash decodes backslash-escapes in `$'...'` 字符串。有关详细信息,请参阅 https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html。 (3认同)
  • 使用Homebrew来获取dos2unix和unix2dos软件包。 (2认同)

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)

  • 关于这些脚本的令人敬畏的事情是它们用正则表达式显示了从任何东西开始转换为任一格式所需的行尾转换所需的内容. (5认同)

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)

  • 实际上,"brew install unix2dos"或"brew install dos2unix"工作正常.他们安装相同的包.使用与你说话的名字:) (9认同)
  • 对于现在遇到此问题的任何人来说,Homebrew公式现在称为"dos2unix".你想要'bre​​w install dos2unix`. (8认同)
  • 或者[Macports](https://www.macports.org/):`port install dos2unix`. (2认同)

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)

  • 试过 perl 和 sed,没有用(我本来可以弄清楚的,不值得一试)。这很好用。 (2认同)

AAv*_*rin 5

  1. 使用自制软件安装dos2unix
  2. 运行find ./ -type f -exec dos2unix {} \;以递归方式转换当前文件夹中的所有行尾


Ste*_*uan 5

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)