Preface
How do I convert DOS newlines CR/LF to Unix/Linux format? To converts text files between DOS and Unix formats you need to use special utility calleddos2unix. DOS text files traditionally have carriage return (\r) and line feed (\n) pairs as their newline characters while Unix text files have the line feed as their newline character.
How-To
UNIX/Linux Commands
You can use the following tools:
Task: Convert DOS file to UNIX format
Consider we have a file called myfile.txt in windows. Please transfer it to Linux machine and use "vi -b myfile.txt" to edit it and using ":set list" to show line feedand carriage return:
("$"-> line feed; "^M"->carriage return)
Type the following command to convert file
However above command will not make a backup of original file myfile.txt. To make a backup of original file. The original file is renamed with the original filename and a .bak extension. Type the following command:
Task: Convert UNIX file to DOS format
Type the following command to convert file called myfile_unix.txt:
Task: Convert Dos TO Unix Using tr Command
Type the following command:
Task: Convert Dos TO Unix Using Perl One Liner
Type the following command:
Task: Convert UNIX to DOS format using sed command
Type the following command if you are using bash shell:
Task: Convert DOS newlines (CR/LF) to Unix format using sed command
Type the following command if you are using bash shell:
(press Ctrl-V then Ctrl-M to get pattern or special symbol)
Toolkit: Transfer To Win/Mac/Lin EOL
Below is a toolkit which can help you to switch text mode file into Win/Mac/Lin EOL without considering EOF of your input file. Let's check the usage:
So if you have a file called myfile.txt in windows. You can try below commands to switch to different type of EOF:
Below is the source code of seof.py:
- # Input validation
- newline=None
- if len(sys.argv) < 4:
- print("Usage: {0} input output type(win|mac|lin)".format(sys.argv[0]))
- exit(1)
- type = {"win":[0x0d,0x0a], "mac":[0x0d], "lin":[0x0a]}
- if not os.path.exists(sys.argv[1]):
- print("Error: Inputfile={0} doesn't exist!".format(sys.argv[1]))
- exit(1)
- if sys.argv[3] not in type.keys():
- print("Error: Wrong type='{0}'!".format(sys.argv[3]))
- exit(1)
- else:
- newline = type[sys.argv[3]]
- print("\t[Info] Newline={0}...".format(newline))
- # Analyze input
- # http://www.ascii-code.com/
- outbuf = []
- bc=0
- with open(sys.argv[1], "rb") as inf:
- b = inf.read(1)
- while b != "":
- bc+=1
- if ord(b) == 10:
- outbuf = outbuf + newline
- elif ord(b)==13:
- outbuf = outbuf + newline
- nc = inf.read(1)
- if nc!="":
- bc+=1
- if ord(nc) != 10:
- outbuf.append(nc)
- else:
- outbuf.append(b)
- b = inf.read(1)
- print("\t[Info] Process %d byte(s) from %s..." % (bc, sys.argv[1]))
- print("\t[Info] Output %d byte(s) to %s..." % (len(outbuf), sys.argv[2]))
- # Output
- with open(sys.argv[2], "wb") as of:
- of.write(bytearray(outbuf))
* Advanced Vi Cheat Sheet
沒有留言:
張貼留言