正确的换行符让Linux和Windows愉快的玩耍

  • 原创
  • Madman
  • /
  • /
  • 0
  • 13597 次阅读

Linux系统转换文本文件的换行符-min.png

Synopsis: 不同操作系统中的换行符标志不一样,在 ASCII 编码中以 LF (NL line feed, new line) 换行键和 CR (carriage return) 回车键来控制文本换行,即我们经常说的 \n 和 \r。Windows下面保存的文本文件默认换行符是 CRLF,而 Linux 是 LF,所以 Windows 下面的文件放到 Linux 系统中无法被打开,本文介绍一些常用方法来查看当前文件使用的是哪种文本换行符,以及如何进行转换

1. 换行符

不同操作系统中的换行符标志不一样,在ASCII编码中以LF (NL line feed, new line)换行键CR (carriage return)回车键来控制文本换行,即我们经常说的\n\r

  • Unix和类Unix(如Linux): \n
  • Windows(MS-DOS): \r\n
  • Mac OS X: \n
  • Mac OS 9: \r

Windows文本在Linux中无法执行:

使用Windows系统中Sublime Text 3编辑器写的脚本传输到Linux中(不是复制内容),导致脚本无法运行

[root@CentOS ~]$ ./test.sh 
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory

2. Linux查看文本换行符

2.1 cat命令

使用选项-A来显示换行符

[root@CentOS ~]$ cat -A test.sh 
#!/bin/bash^M$
# Author: Madman^M$
# Date: 2017/03/06^M$
# Email: wangy8961@163.com^M$
# Version: 1.0^M$
^M$
echo "Test shell file"^M$

2.2 vi/vim命令

使用选项-b以Binary mode打开文件,来显示换行符

[root@CentOS ~]$ vim -b test.sh 

#!/bin/bash^M
# Author: Madman^M
# Date: 2017/03/06^M
# Email: wangy8961@163.com^M
# Version: 1.0^M
^M
echo 
                                
                            
分类: Linux
标签: cat CRLF dos2unix LF tr vim sed
  • Vash login obnovlen www.anallador8.temp.swtest.ru qo
  • Vash schet pod voprosom www.ch912437.tw1.ru 1k
  • Vash dostup usilen www.cn090411.tw1.ru Qt
  • Vam otpravleny dokumenty www.anallador7.temp.swtest.ru 9e
  • Vam povysili kreditnyj limit www.anallador8.temp.swtest.ru Gl
  • Vtoroj faktor trebuetsya www.anallador9.temp.swtest.ru xP
  • Vash schet na proverke www.anallador7.temp.swtest.ru SJ
  • Easily Secure Your Monetary Payout anallador5.temp.swtest.ru ZE
  • yequn98
  • Your jackpot win awaits today anallador6.temp.swtest.ru Ug
  • Vash balans izmenilsya www.ch912437.tw1.ru WN
  • Vash nalogovyj vychet www.anallador7.temp.swtest.ru Pd
  • Vam otkryt dostup k sektsii www.anallador8.temp.swtest.ru 82
  • Vash bonus perezagruzhen www.anallador8.temp.swtest.ru ql
  • Srochno trebuetsya vash vybor www.cz475182.tw1.ru Ay
  • Vashu dverj vskryli www.anallador9.temp.swtest.ru aS
  • Get lucky and win money instantly anallador5.temp.swtest.ru 3O
  • Zaem odobren bankom www.anallador9.temp.swtest.ru p2
  • Poslednee preduprezhdenie www.cz475182.tw1.ru Da
  • The hidden cache is now open anallador6.temp.swtest.ru wi
  • Discover your anniversary surprise anallador4.temp.swtest.ru Ns
  • Being chosen means you get money anallador4.temp.swtest.ru NJ
  • Don’t Refuse This Free Money Gift anallador6.temp.swtest.ru 60
  • Retrieve your prize of fortune anallador4.temp.swtest.ru 0z
未经允许不得转载: LIFE & SHARE - 王颜公子 » 正确的换行符让Linux和Windows愉快的玩耍

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列