将换行符格式从 Mac 转换为 Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6373888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:28:53  来源:igfitidea点击:

Converting newline formatting from Mac to Windows

windowsmacosformattingnewlineutility

提问by Yarin

I need a conversion utility/script that will convert a .sql dump file generated on Mac to one readable on Windows. This is a continuation of a problem I had here. The issue seems to be with newline formatting in text files, but I can't find a tool to make the conversion...

我需要一个转换实用程序/脚本,它将 Mac 上生成的 .sql 转储文件转换为 Windows 上可读的文件。这是我在这里遇到的问题的延续。问题似乎与文本文件中的换行符格式有关,但我找不到进行转换的工具...

回答by Anne

Windows uses carriage return+ line feedfor newline:

Windows 使用carriage return+line feed作为换行符:

\r\n

Unix only uses Line feedfor newline:

Unix 只Line feed用于换行:

\n

In conclusion, simply replace every occurence of \nby \r\n.
Both unix2dosand dos2unixare not by default available on Mac OSX.
Fortunately, you can simply use Perlor sedto do the job:

总之,只需替换每次出现的\nby \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

Code snippet from:
http://en.wikipedia.org/wiki/Newline#Conversion_utilities

代码片段来自:http:
//en.wikipedia.org/wiki/Newline#Conversion_utilities

回答by JosephH

This is an improved version of Anne's answer -- if you use perl, you can do the edit on the file 'in-place' rather than generating a new file:

这是安妮答案的改进版本——如果你使用 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

回答by Steven Hirlston

You can install unix2dos with Homebrew

您可以使用Homebrew安装 unix2dos

brew install unix2dos

Then you can do this:

然后你可以这样做:

unix2dos file-to-convert

You can also convert dos files to unix:

您还可以将 dos 文件转换为 unix:

dos2unix file-to-convert

回答by Paul R

You probably want unix2dos:

你可能想要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.

You can either run unix2doson your DOS/Windows machine using cygwinor on your Mac using MacPorts.

您可以unix2dos使用cygwin在DOS/Windows 机器上运行,也可以使用MacPorts在 Mac 上运行。

回答by parahren

Just do trdelete:

只需tr删除:

tr -d "\r" <infile.txt >outfile.txt

回答by AAverin

  1. Install dos2unix with homebrew
  2. Run find ./ -type f -exec dos2unix {} \;to recursively convert all line-endings within current folder
  1. 使用自制软件安装 dos2unix
  2. 运行find ./ -type f -exec dos2unix {} \;以递归转换当前文件夹中的所有行尾

回答by Stephen Quan

vimalso can convert files from UNIX to DOS format. For example:

vim还可以将文件从 UNIX 转换为 DOS 格式。例如:

vim hello.txt <<EOF
:set fileformat=dos
:wq
EOF

回答by ECJB

The following is a complete script based on the above answers along with sanity checking and works on Mac OS X and should work on other Linux / Unix systems as well (although this has not been tested).

以下是基于上述答案以及健全性检查的完整脚本,适用于 Mac OS X,也适用于其他 Linux / Unix 系统(尽管尚未经过测试)。

#!/bin/bash

# http://stackoverflow.com/questions/6373888/converting-newline-formatting-from-mac-to-windows

# =============================================================================
# =
# = FIXTEXT.SH by ECJB
# =
# = USAGE:  SCRIPT [ MODE ] FILENAME
# =
# = MODE is one of unix2dos, dos2unix, tounix, todos, tomac
# = FILENAME is modified in-place
# = If SCRIPT is one of the modes (with or without .sh extension), then MODE
# =   can be omitted - it is inferred from the script name.
# = The script does use the file command to test if it is a text file or not,
# =   but this is not a guarantee.
# =
# =============================================================================

clear
script="
cat foo | col -b > foo2
" modes="unix2dos dos2unix todos tounix tomac" usage() { echo "USAGE: $script [ mode ] filename" echo echo "MODE is one of:" echo $modes echo "NOTE: The tomac mode is intended for old Mac OS versions and should not be" echo "used without good reason." echo echo "The file is modified in-place so there is no output filename." echo "USE AT YOUR OWN RISK." echo echo "The script does try to check if it's a binary or text file for sanity, but" echo "this is not guaranteed." echo echo "Symbolic links to this script may use the above names and be recognized as" echo "mode operators." echo echo "Press RETURN to exit." read answer exit } # -- Look for the mode as the scriptname mode="`basename "
sed -e 's/^M$//' -i '' filename
" .sh`" fname="" # -- If 2 arguments use as mode and filename if [ ! -z "" ] ; then mode=""; fname=""; fi # -- Check there are 1 or 2 arguments or print usage. if [ ! -z "" -o -z "" ] ; then usage; fi # -- Check if the mode found is valid. validmode=no for checkmode in $modes; do if [ $mode = $checkmode ] ; then validmode=yes; fi; done # -- If not a valid mode, abort. if [ $validmode = no ] ; then echo Invalid mode $mode...aborting.; echo; usage; fi # -- If the file doesn't exist, abort. if [ ! -e "$fname" ] ; then echo Input file $fname does not exist...aborting.; echo; usage; fi # -- If the OS thinks it's a binary file, abort, displaying file information. if [ -z "`file "$fname" | grep text`" ] ; then echo Input file $fname may be a binary file...aborting.; echo; file "$fname"; echo; usage; fi # -- Do the in-place conversion. case "$mode" in # unix2dos ) # sed does not behave on Mac - replace w/ "todos" and "tounix" # # Plus, these variants are more universal and assume less. # sed -e 's/$/\r/' -i '' "$fname" # UNIX to DOS (adding CRs) # ;; # dos2unix ) # sed -e 's/\r$//' -i '' "$fname" # DOS to UNIX (removing CRs) # ;; "unix2dos" | "todos" ) perl -pi -e 's/\r\n|\n|\r/\r\n/g' "$fname" # Convert to DOS ;; "dos2unix" | "tounix" ) perl -pi -e 's/\r\n|\n|\r/\n/g' "$fname" # Convert to UNIX ;; "tomac" ) perl -pi -e 's/\r\n|\n|\r/\r/g' "$fname" # Convert to old Mac ;; * ) # -- Not strictly needed since mode is checked first. echo Invalid mode $mode...aborting.; echo; usage ;; esac # -- Display result. if [ "$?" = "0" ] ; then echo "File $fname updated with mode $mode."; else echo "Conversion failed return code $?."; echo; usage; fi

回答by patdevelop

Here's a really simple approach, worked well for me, courtesy Davy Schmeits's Weblog:

这是一个非常简单的方法,对我来说效果很好,由Davy Schmeits 的博客提供

##代码##

Where foo is the file that has the Control+M characters at the end of the line, and foo2 the new file you are creating.

其中 foo 是行尾带有 Control+M 字符的文件,而 foo2 是您正在创建的新文件。

回答by Olga

On Yosemite OSX, use this command:

在 Yosemite OSX 上,使用以下命令:

##代码##

where the ^Msequence is achieved by pressing Ctrl+Vthen Enter.

其中^M序列是通过按Ctrl+ Vthen来实现的Enter