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

來源:互聯網
上載者:User

 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.