1. Conversion in VI Editor
Use command set FF in VI? (FileFormat) You can view the format of a file, use Set Ff=dos (Unix) to format the file
2. Processing using sed (come to http://www.cnblogs.com/yangyh/archive/2011/08/12/linux.html)
SED command notes
Tag:linux SED Regular replacement
Dos/windows and Linux/unix have different file line return formats, Dos/windows-based text files have a CR (carriage return) and LF (line break) at the end of each line, and UNIX text has only one line break.
1) Move the file under Dos/windows to the Linux/unix system
Although many programs do not care about the cr/lf text file in dos/windows format, there are several programs that care-the most famous is bash, which can be problematic whenever you encounter a carriage return. The following SED call will convert text in dos/windows format to a trustworthy UNIX format:
$ Sed-e ' s/.$//' mydos.txt > Myunix.txt
Replace the last character of each line with an empty one
$ Sed-e ' s/.$/abc/' mydos.txt > Myunix.txt
Replace the last character of each line with ABC
The script works very simply: an override rule expression matches the last character of a line, and that character happens to be a carriage return. We replace it with a null character so that it is completely removed from the output. If you use the script and notice that the last character of each line in the output has been deleted, you have specified a text file that is already in UNIX format. There's no need to do that!
2) Move the Linux/unix text to the Windows system and use the following script to perform the required format conversions:
$ Sed-e ' s/$/\r/' myunix.txt > Mydos.txt
Add \ r at the end of each line
$ Sed-e ' s/123$/345/' myunix.txt > Mydos.txt
Each line ends with a line of 123, replacing 123 with 345
In the script, the ' $ ' rule expression will match the end of the line, and '/R ' tells SED to insert a carriage return before it. Insert a carriage return before line break, and immediately, each line ends with CR/LF. Please note that CR is replaced with ' \ R ' only when using the GNU sed 3.02.80 or later versions.
=====================================================================
How DOS file formats convert to UNIX file formats
DOS format files to the UNIX system, will be at the end of each line more than one ^m, of course, it may not be visible, but in the VI, the following shows the format of this file, such as "Dos.txt" [dos] 120L, 2532C, said to be a [DOS] format file, If it is a Mac system, it will display [Mac], because the file format of the cause sometimes cause our Unix program, or shell program error, then need to convert these DOS file format to UNIX format, by
VI Dos.txt
: Set Fileformat=unix
: W
This file is converted to a UNIX format file, which is typically written on a Windows machine with files uploaded to Unix.
With the command: set FF?
You can see the Word dos or UNIX.
Use: Set Ff=unix to force it to UNIX format
It can also be done using tools such as sed:
Sed ' s/^m//' filename > Tmp_filename
Where ^m is at the same time Ctrl+v+m pressed out, indicating carriage return.
Dos,unix format processing difference: DOS is a carriage return + line, while under UNIX, only carriage return, no line change!
Sed-e ' s/a/a/' T.txt > T1.txt turns the first a in the t.txt of each row into a
Sed-e ' s/a/a/g ' t.txt > T1.txt convert all A in a row of t.txt to a
3) How to mix Windows and UNIX formats into files that have become UNIX-formatted
Sed-e ' s/\r//g ' window.txt > Unix.txt
Conversion between DOS and UNIX file formats