Macで生成された.sqlダンプファイルをWindowsで読み取り可能なファイルに変換する変換ユーティリティ/スクリプトが必要です。これは私がここで抱えていた問題の続きです。問題はテキストファイルの改行フォーマットにあるようですが、変換を行うためのツールが見つかりません...
12 に答える
Windowsは改行にcarriage return
+を使用します:line feed
\r\n
UnixはLine feed
改行にのみ使用します:
\n
結論として、のすべての出現を。で置き換えるだけ\n
です\r\n
。とは
両方とも、 MacOSXではデフォルトでは使用できません。
幸いなことに、あなたは単にまたは仕事をするために使うことができます:unix2dos
dos2unix
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
これは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
あなたはHomebrewでunix2dosをインストールすることができます
brew install unix2dos
次に、これを行うことができます:
unix2dos file-to-convert
dosファイルをunixに変換することもできます。
dos2unix file-to-convert
削除してtr
ください:
tr -d "\r" <infile.txt >outfile.txt
あなたはおそらく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.
cygwinunix2dos
を使用してDOS/Windowsマシンで実行するか、MacPortsを使用してMacで実行できます。
- 自作でdos2unixをインストールする
find ./ -type f -exec dos2unix {} \;
現在のフォルダ内のすべての行末を再帰的に変換するために実行します
vim
また、ファイルをUNIXからDOS形式に変換することもできます。例えば:
vim hello.txt <<EOF
:set fileformat=dos
:wq
EOF
これは本当に単純なアプローチで、私にとってはうまくいきました。DavySchmeitsのウェブログの礼儀です:
cat foo | col -b > foo2
ここで、fooは行末にControl + M文字が含まれるファイルであり、foo2は作成する新しいファイルです。
以下は、健全性チェックとともに上記の回答に基づく完全なスクリプトであり、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
Yosemite OSXでは、次のコマンドを使用します。
sed -e 's/^M$//' -i '' filename
ここで、シーケンスは+を押してから。^M
を押すことによって達成されます。CtrlVEnter
怠惰すぎてperl-one-linerを入力できないので、短いperlスクリプトでperlを使用して、AnneとJosephHの回答を拡張します。
たとえば「unix2dos.pl」という名前のファイルを作成し、パスのディレクトリに配置します。2行を含むようにファイルを編集します。
#!/usr/bin/perl -wpi
s/\n|\r\n/\r\n/g;
「whichperl」がシステム上で「/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