一、编写
在Windows系统中,用记事本编写如下内容,保存为win.txt:
this is a text file.
how are you?
ok!
see you.
编码格式设置为UTF-8
在centos7中,用vim编写同样内容的文件,保存为linux.txt
用xftp传输文件:把linux.txt传到win.txt同一文件夹中;把win.txt传到linux.txt同一目录下。
二、显示
在Windows下同时打开两文件:
在centos7中用cat命令同时查看两文件内容:
在centos7中用cat -A命令同时查看两文件内容:
三、区别
1.Windows中只能一次查看一个文件,不能同时查看两个文件
也不能分窗口显示,只能新建窗口
2.Windows文本文件有头部标记
M-Om-;M-?
而linux文本文件没有头部标记
3.Windows文本文件换行符是\r\n,在linux中用cat –A查看时呈现^M$
linux文本文件换行符是\n,用cat –A查看时是$
四、转化
1.工具转化
a.在linux中下载软件
yum –y install unix2dos
b.Windows文本文件转为linux文本文件:
dos2unix 文件名
c.linux文本文件转为Windows文本文件
unix2dos 文件名
2.执行命令
windows文本文件转换为linux文本文件
a.grep -->bom
grep -r -I -l $'^\xEF\xBB\xBF' ./
这是查找出是否含有头部标记
b.find delete_bom
find ./ -type f -exec sed -i 's/\xEF\xBB\xBF//' {} \;
这是找到文件,删除头部标记
c.转化\r\n 为\n
查看windows文本文件:
运用命令 cat -A 文件名
可看到 每行最后都有^M$
我们需要删除这个最后的标记
sed -i 's/.$//g' 文件名
完成以上几步后,再查看文本文件,就是linux中正常的了!!!
总结
执行一条命令,用sed把win文件转为linux文件
应用扩展正则,要加参数-r 修改源文件,要加参数 -i
sed -i -r 's/(\xEF\xBB\xBF|.$)//' 文件名
sed -i -r 's/\xEF\xBB\xBF|.$//' 文件名
不用扩展正则
sed -i 's/\xEF\xBB\xBF//;s/.$//' 文件名
linux文本文件转换为windows文本文件
原windows文件
sed -i "s/$/\r/g" 文件名
(转化后的windows文本文件没有头标记)
3.vim转化
windows文本文件转换为linux文本文件
用vim打开文件,设置格式转化
:set ff=unix
保存文件,即可。用cat –A命令查看,可以看到已转化为linux格式。
linux文本文件转换为windows文本文件
用vim打开文件,设置格式转化
:set ff=dos
保存文件,即可。用cat –A命令查看,可以看到已转化为windows格式。
五、效果
直接打开linux文本文件
转化后的linux文本文件