linux(unix)文件格式类型以及dos和unix文件格式的转换

首先我们来看下linux下是如何识别文件格式的。unix,dos,mac都分别有自己使用的换行符。
The line terminator expected for each file format is:
unix LF only (each line ends with an LF character).
dos CRLF (each line ends with two characters, CR then LF).
mac CR only (each line ends with a CR character).
CR is carriage return (return cursor to left margin), which is Ctrl-M or ^M or hex 0D.
LF is linefeed (move cursor down), which is Ctrl-J or ^J or hex 0A. Sometimes, LF is written as NL (newline).

在linux shell里做个试验。 (注意^M符号通过ctrl+v 然后输入 ctrl+M)

试验一:
[root@ ~]# echo "hello dos^M" >> test1
[root@ ~]# echo "hello unix" >> test1
[root@ ~]# vi test1
打开后你砍到了什么?
vi里面输入:set ff
看看vi编辑器默认认为的文件格式是什么?

试验二:

[root@ ~]# echo "hello unix" >> test2
[root@ ~]# echo "hello dos^M" >> test2
[root@ ~]# vi test2
打开后你砍到了什么?
vi里面输入:set ff
看看vi编辑器默认认为的文件格式是什么?

试验三:

[root@ ~]# echo "hello dos^M" >> test3
[root@ ~]# echo "hello dos^M" >> test3
[root@ ~]# vi test3
打开后你砍到了什么?
vi里面输入:set ff
看看vi编辑器默认认为的文件格式是什么?

从这里看出,如果文件中所有的换行符(行结束符),只要包含一个unix 的换行符,则vim就默认为是unix文件格式。 这时如果有dos的换行符,则会导致显示^M的显示。 而这个符号可能会导致bash脚本或者程序解析错误发生。 如果所有的换行符都是dos类型换行符,则vim还是会认为是dos文件格式。 所以vim是优先识别unix文件格式的。毕竟linux是unix的分支嘛。

由于dos文件格式和unix的不同,所以我们又时候需要转换文件格式类型。

Task: Convert DOS file to UNIX format

Type the following command to convert file called myfile.txt:
$ dos2unix myfile.txt

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:
$ dos2unix -b myfile.txt

Task: Convert UNIX file to DOS format

Type the following command to convert file called myfile.txt:
$ unix2dos myfile.txt
$ unix2dos -b myfile.txt

Task: Convert Dos TO Unix Using tr Command

Type the following command:

tr -d ‘\r’ < input.file > output.file

Task: Convert Dos TO Unix Using Perl One Liner

Type the following command:

perl -pi -e ‘s/\r\n/\n/g’ input.file

Task: Convert UNIX to DOS format using sed command

Type the following command if you are using bash shell:
$ sed ‘s/$'”/`echo \\\r`/” input.txt > output.txt

Note: sed version may not work under different UNIX/Linux variant,refer your local sed man page for more info.

Task: Convert DOS newlines (CR/LF) to Unix format using sed command

If you are using BASH shell type the following command (press Ctrl-V then Ctrl-M to get pattern or special symbol)
$ sed ‘s/^M$//’ input.txt > output.txt

Note: sed version may not work under different UNIX/Linux variant, refer your local sed man page for more info.

此篇文章已被阅读3698 次

Add a Comment

邮箱地址不会被公开。 必填项已用*标注