ฉันต้องการยูทิลิตี้ / สคริปต์การแปลงที่จะแปลงไฟล์ดัมพ์. sql ที่สร้างบน Mac ให้เป็นไฟล์ที่อ่านได้บน Windows นี้เป็นความต่อเนื่องของปัญหาผมมีที่นี่ ปัญหาน่าจะเกิดจากการจัดรูปแบบขึ้นบรรทัดใหม่ในไฟล์ข้อความ แต่ฉันไม่พบเครื่องมือที่จะทำการแปลง ...
คำตอบ:
Windows ใช้carriage return+ line feedสำหรับการขึ้นบรรทัดใหม่:
\r\n
Unix ใช้Line feedสำหรับขึ้นบรรทัดใหม่เท่านั้น:
\n
โดยสรุปเพียงแทนที่เกิดขึ้นของทุกโดย\n
ทั้งสองอย่างและไม่มีตามค่าเริ่มต้นใน Mac OSX
โชคดีที่คุณสามารถใช้หรือทำงาน:\r\nunix2dosdos2unixPerlsed
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: sed $'s/\r$//'Explanation: bash ถอดรหัสแบ็กสแลช - เอสเคปใน$'...'สตริง ดูรายละเอียดได้ที่gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
นี่เป็นคำตอบของ Anne รุ่นปรับปรุง - หากคุณใช้ perl คุณสามารถแก้ไขไฟล์ 'in-place' ได้แทนที่จะสร้างไฟล์ใหม่:
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 ด้วยHomebrew
brew install unix2dos
จากนั้นคุณสามารถทำได้:
unix2dos file-to-convert
คุณยังสามารถแปลงไฟล์ dos เป็น unix:
dos2unix file-to-convert
dos2unixเรียกว่าตอนนี้ brew install dos2unixคุณจะต้องการ
brew install unix2dosหรือbrew install dos2unixทำงานได้ดี พวกเขาติดตั้งแพ็คเกจเดียวกัน ใช้ชื่อใดก็ได้ที่พูดถึงคุณ :)
คุณอาจต้องการ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หรือบน Mac ของคุณโดยใช้MacPorts
เพียงแค่trลบ:
tr -d "\r" <infile.txt >outfile.txt
wc -l)
trคำสั่งเพื่อทำการแปลงต่างๆ ใช้hexdumpหรือคล้ายกันเพื่อค้นหาว่าตอนนี้ใช้รูปแบบการประชุม end-of-line ประเภทใดในไฟล์
find ./ -type f -exec dos2unix {} \;เพื่อแปลงการสิ้นสุดบรรทัดทั้งหมดซ้ำ ๆ ภายในโฟลเดอร์ปัจจุบันvimยังสามารถแปลงไฟล์จาก UNIX เป็นรูปแบบ DOS ตัวอย่างเช่น:
vim hello.txt <<EOF
:set fileformat=dos
:wq
EOF
ต่อไปนี้เป็นสคริปต์ที่สมบูรณ์ตามคำตอบข้างต้นพร้อมกับการตรวจสอบความถูกต้องและทำงานบน 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
นี่เป็นวิธีการที่ง่ายมากใช้ได้ดีสำหรับฉันWeblog ของ Davy Schmeits :
cat foo | col -b > foo2
โดย foo คือไฟล์ที่มีอักขระ Control + M ที่ท้ายบรรทัดและ foo2 ไฟล์ใหม่ที่คุณกำลังสร้าง
บน Yosemite OSX ให้ใช้คำสั่งนี้:
sed -e 's/^M$//' -i '' filename
ที่^Mลำดับที่จะทำได้โดยการกดCtrl+ แล้วVEnter
sed ไม่เข้าใจทับขวา-หนีเช่น\rและ `` \ n` และดังนั้นจึงยังสามารถใช้เหล่านี้ในการทดแทน คุณไม่จำเป็นต้องป้อนตัวควบคุม -M ตามตัวอักษรเพื่ออ้างถึงอักขระนั้น (หรืออื่น ๆ ) หลักการของการใช้sed(และ-i) ที่จะทำใด ๆเรียงลำดับของการแปลงชนิดนี้เป็นหนึ่งที่ดีมากเพราะไม่เหมือนtrคุณจะไม่ได้ จำกัด อยู่ที่ "ตัวละครตัวหนึ่งในเวลา."
ขยายคำตอบของ Anne และ JosephH โดยใช้ perl ในสคริปต์ perl สั้น ๆ เนื่องจากฉันขี้เกียจพิมพ์ perl-one-liner เป็นเวลานานมาก
สร้างไฟล์ชื่อตัวอย่างเช่น "unix2dos.pl" และวางไว้ในไดเร็กทอรีในเส้นทางของคุณ แก้ไขไฟล์ให้มี 2 บรรทัด:
#!/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 สวัสดี \ n
$ unix2dos.pl xxx
$ od -c xxx (ตรวจสอบว่าตอนนี้ลงท้ายด้วย cr lf)
0000000 สวัสดี \ r \ n