将换行格式从Mac转换为Windows

问题描述 投票:120回答:12

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

windows macos formatting newline utility
12个回答
126
投票

Windows使用carriage return + line feed换行:

\r\n

Unix只使用Line feed换行:

\n

总之,简单地用\n替换\r\n的每一次出现。 在Mac OSX上默认不提供unix2dosdos2unix。 幸运的是,你可以简单地使用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

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


0
投票

这是一个非常简单的方法,对我来说很好,礼貌cat foo | col -b > foo2

#!/usr/bin/perl -wpi
s/\n|\r\n/\r\n/g;

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


0
投票

扩展了Anne和JosephH的答案,在一个简短的perl脚本中使用perl,因为我懒得很快就输入perl-one-liner。 创建一个名为“unix2dos.pl”的文件,并将其放在路径中的目录中。编辑文件以包含2行:

screendump from XCode

假设“which perl”在您的系统上返回“/ usr / bin / perl”。使文件可执行(chmod u + x unix2dos.pl)。

例: $ echo“hello”> xxx $ od -c xxx(检查文件是否以nl结尾) 0000000 h e l l o \ n

$ unix2dos.pl xxx $ od -c xxx(检查它现在以cr lf结束) 0000000 h e l l o \ r \ n


0
投票

在左侧面板的Xcode 9中打开/选择项目导航器中的文件。如果文件不存在,请将其放入项目导航器中。

在右侧面板中找到“文本设置”并将“行结束”更改为Windows(CRLF)。

XCode screendumpqazxswpoi


121
投票

这是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

103
投票

您可以使用Homebrew安装unix2dos

brew install unix2dos

然后你可以这样做:

unix2dos file-to-convert

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

dos2unix file-to-convert

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.

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


13
投票

只是做tr -d "\r" <infile.txt >outfile.txt 删除:

find ./ -type f -exec dos2unix {} \;

4
投票
  1. 用自制软件安装dos2unix
  2. 运行vim以递归方式转换当前文件夹中的所有行结尾

2
投票

vim hello.txt <<EOF :set fileformat=dos :wq EOF 还可以将文件从UNIX转换为DOS格式。例如:

#!/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="$0"
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 "$0" .sh`"
fname="$1"

# -- If 2 arguments use as mode and filename
if [ ! -z "$2" ] ; then mode="$1"; fname="$2"; fi

# -- Check there are 1 or 2 arguments or print usage.
if [ ! -z "$3" -o -z "$1" ] ; 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

2
投票

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

sed -e 's/^M$//' -i '' filename

1
投票

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

^M

通过按Ctrl + V然后按Enter实现Davy Schmeits's Weblog序列。

© www.soinside.com 2019 - 2024. All rights reserved.