现在的位置: 首页 > 综合 > 正文

【整理】如何取消Linux下,vi中显示的^M符号

2013年05月08日 ⁄ 综合 ⁄ 共 3029字 ⁄ 字号 评论关闭
 from:http://apps.hi.baidu.com/share/detail/30042616
【整理】如何取消Linux下,vi中显示的^M符号
【背景知识】
^M 是ascii中的\'\\r\', 回车符,是16进制的0x0D,8进制的015,十进制的13。
对于换行这个动作,unix下一般只有一个0x0A表示换行,windows下一般都是0x0D和0x0A两个字符。
另外:^L 是ascii 0x0C \'\\f\', 换页控制符。
而对于Linux 的vi,有些版本,比如我当前开发板里面的vi,是用busybox编译出来的,不能识别windows下面编辑的,带0x0D的那些文本文件,所以,你在windows下面编辑一个普通的文本文件,如果里面有换行,那么在Linux的vi里面,对应的那个0x0D就会显示出一个^M,比如:
1. led on & off^M
eg.^M
echo none > /sys/class/leds/led_green/trigger^M
echo 1 > /sys/class/leds/led_green/brightness^M
echo > /sys/class/leds/led_green/brightness^M
Note: the default switch of trigger is [none]^M
2. heartbeat flash^M
eg.^M
echo heartbeat > /sys/class/leds/led_green/trigger^M
3. timer flash^M
eg.^M
echo timer > /sys/class/leds/led_green/trigger^M
echo 5 > /sys/class/leds/led_green/delay_on^M
echo 5 > /sys/class/leds/led_green/delay_off^M
每行最后都有一个^M,很是难看,所以想要去掉。
注:普通PC版本的Linux,好像可以很好地识别了,比如我的Linux服务器,OpenSUSE下面的某个版本vi的,可以很好地显示那些windows下面编辑的,带0x0D的回车换行符,不能出现^M。
此外,关于ASCII字符,Linux下面有个简要的介绍:
[crifan@linux-41lh wi-fi]$man ascii
ASCII(7)                                            Linux Programmer\'s Manual                                           ASCII(7)
 
NAME
       ascii - the ASCII character set encoded in octal, decimal, and hexadecimal
 
DESCRIPTION
       ASCII is the American Standard Code for Information Interchange. It is a 7-bit code. Many 8-bit codes (such as ISO 8859-1, the Linux default character set) contain ASCII as their lower half. The international counterpart of ASCII is known as ISO 646.
 
       The following table contains the 128 ASCII characters.
 
       C program \'\\X\' escapes are noted.
 
       Oct   Dec   Hex   Char                        Oct   Dec   Hex   Char
       ------------------------------------------------------------------------
       000   0     00    NUL \'\\0\'                    100   64    40    @
       001   1     01   SOH (start of heading)      101   65    41    A
       002   2     02    STX (start of text)         102   66    42    B
       003   3     03    ETX (end of text)           103   67    43    C
       004   4     04    EOT (end of transmission)   104   68    44    D
       005   5     05    ENQ (enquiry)               105   69    45    E
       006   6     06    ACK (acknowledge)           106   70    46    F
       007   7     07    BEL \'\\a\' (bell)             107   71    47    G
       010   8     08   BS \'\\b\' (backspace)        110   72    48    H
       011   9     09    HT \'\\t\' (horizontal tab)   111   73    49    I
       012   10    0A    LF \'\\n\' (new line)         112   74    4A    J
       013   11    0B    VT \'\\v\' (vertical tab)     113   75    4B    K
       014   12    0C    FF \'\\f\' (form feed)        114   76    4C    L
       015   13    0D    CR \'\\r\' (carriage ret)     115   77    4D    M
       016   14    0E    SO (shift out)             116   78    4E    N
       017   15    0F    SI (shift in)              117   79    4F    O
。。。。。。。。。。。
 
【如何消除vi中的^M】
我刚开始去用sed命令:
sed \'s/^M//g\' file_old > file_new
不成功,输出的文件,用vi打开,还是带^M。
后来到网上搜到了可以用的办法,用tr命令:
 
tr -d \"\\015\" < myfile.txt > myfile_new.txt
 
可以把符号^M(即\"\\015\")去掉,并另存为新文件myfile_new.txt
【后记】
根据别人的建议,知道了还有个专门的工具dos2unix和unix2dos,用于处理这方面的事情,详情参考:
【转】dos2unix和unix2dos命令使用
http://hi.baidu.com/serial_story/blog/item/ddc783314af738a55fdf0ee2.html
其用法很简单,直接使用即可:
dos2unix file_name
这样就可以把该文件里面的0x0D去掉了,只保留0x0A。
【引用】
1。如何取消vi中显示的^M符号
http://hi.baidu.com/nourewang/blog/item/7d2f9e84069d6e3466096e1a.html
2。Linux 下用程序新建的文件中出现^M字符,如何消除??
http://www.linuxforum.net/forum/printthread.php?Cat=&Board=program&main=32848&type=thread
3。[请教]用vi编辑文本时出现的^M和^L代表什么?
http://www.linuxsir.org/bbs/thread305370.html

抱歉!评论已关闭.