Dos和Unix的换行 转换

DOS and UNIX end-of-line characters

The UNIX end-of-line character is a line feed/newline character (/n). The DOS/Windows end-of-line character is a carriage return, followed by a line feed/newline (/r/n).

Most editors can automatically convert these to the appropriate format, but others, such as Notepad in Windows, cannot.

To convert a UNIX file to DOS using sed (GNU sed 3.02.80 or later):
$ sed 's/$//r/' UNIX_file > DOS_file

To convert a DOS file to UNIX file, use tr to remove the carriage return:
$ tr -d '/r' < DOS_file > UNIX_file

To accomplish the same thing using sed:
$ sed 's/^M//' DOS_file > UNIX_file

Note: To generate the ^M above, press Ctrl-V, then Ctrl-M.

FTP's ASCII transfer mode will automatically do the end-of-line conversion between DOS and UNIX systems.

Also, the commands dos2unix and unix2dos may be used to perform the conversion, if present on your system.

  • Emacs

    You can also convert a DOS file named dosfile.txt to a Unix text file using Emacs, a Unix text editor. Enter at the Unix shell prompt:
      emacs dosfile.txt
    This will open the file in the Emacs text editor. To remove all the "^M" characters, type
      M-% C-q C-m RET RET !

    It may also be necessary to remove a Ctrl-z at the end of the document. To quickly get to the end of the document in Emacs, type
      M->
    If you see a Ctrl-z at the end of the document, delete it.

    To convert a Unix file named unixfile.txt to a DOS text file, first open it in Emacs. At the Unix shell prompt, enter
      emacs unixfile.txt
    This will open the file in the Emacs text editor. To add carriage returns, which will show up as ^M in Emacs, type
      M-% C-q C-j RET C-q C-m C-q C-j RET !
    It may also be necessary to add a Ctrl-z at the end of the document. At the very end of the document, press
      C-q C-z
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值