Answers:
Windows 对换行符使用carriage return
+ line feed
:
\r\n
Unix仅Line feed
用于换行符:
\n
总之,只需更换的每一次出现\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
来自以下代码段:http :
//en.wikipedia.org/wiki/Newline#Conversion_utilities
sed
UNIX到DOS 的命令在OS X Lion上对我不起作用-它仅在每行的末尾插入文本“ r”。该perl
命令虽然有效。
sed
,但是您可以在不安装Homebrew,gnu-sed或unix2dos的情况下解决该问题:使用sed -e 's/$/^M/' inputfile > outputfile
,其中^M
在命令行上通过产生的控制字符Ctrl+V Ctrl+M
。
$
在包含sed命令的单引号之前放置a :sed $'s/\r$//'
说明:bash解码$'...'
字符串中的反斜杠转义符。有关详细信息,请参见gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html。
这是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
Can't do inplace edit on file: Permission denied.
,并删除文件。查看其他实用程序。
您可能想要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
使用cygwin在DOS / Windows计算机上运行,也可以使用MacPorts在Mac上运行。
只需tr
删除:
tr -d "\r" <infile.txt >outfile.txt
wc -l
)。
tr
命令执行各种转换。使用hexdump
或类似方法找出文件中现在使用的行尾约定的确切类型。
以下是基于上述答案以及健全性检查的完整脚本,并且可以在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="$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
这是一种非常简单的方法,对我来说很有效,由Davy Schmeits的Weblog提供:
cat foo | col -b > foo2
其中foo是该行末尾具有Control + M字符的文件,而foo2是您要创建的新文件。
在优胜美地OSX上,使用以下命令:
sed -e 's/^M$//' -i '' filename
其中^M
序列通过按压来实现Ctrl+ V然后Enter。
sed
它确实了解反斜杠转义,例如\r
和``\ n`,因此也可以在替换中使用它们。您实际上不必输入文字控件M来引用该字符(或任何其他字符)。使用sed
(和-i
)进行任何类型的转换的原理是一个很好的原理,因为与不同tr
,您不限于“一次一个字符”。
在简短的perl脚本中使用perl扩展了Anne和JosephH的答案,因为我太懒了以至于无法立即输入perl-one-liner。
创建一个文件,例如“ unix2dos.pl”,并将其放在路径中的目录中。编辑文件以包含两行:
#!/usr/bin/perl -wpi
s/\n|\r\n/\r\n/g;
假设“哪个perl”在您的系统上返回“ / usr / bin / perl”。使文件可执行(chmod u + x unix2dos.pl)。
示例:
$ echo“ hello”> xxx
$ od -c xxx(检查文件是否以nl结尾)
0000000 hello \ n
$ unix2dos.pl xxx
$ od -c xxx(检查它现在以cr lf结尾)
0000000 hello \ r \ n