找回密码
 注册

QQ登录

只需一步,快速开始

查看: 487|回复: 0

Convert DOS Newlines CR-LF to Unix/Linux Format

[复制链接]
发表于 2011-8-17 09:24:28 | 显示全部楼层 |阅读模式
To converts text files between DOS and Unix formats you need to use special utility called dos2unix. DOS text files traditionally have carriage return and line feed pairs as their newline characters while Unix text files have the line feed as their newline character.


UNIX/Linux Commands
You can use the following tools:
  • dos2unix (also known as fromdos) -  converts text files from the DOS format to the Unix
    format
  • unix2dos (also known as todos) - converts text files from the Unix format to the DOS format.
  • sed - You can use sed command for same purpose
  • tr command
  • Perl one liner

Task: Convert DOS file to UNIX format
Type the following command to convert file called myfile.txt:
$ dos2unix myfile.txt
However above command will not make a backup of original file myfile.txt. To make a backup  of original file. The original file is renamed with the original filename and a .bak extension. Type the following command:
$ dos2unix -b myfile.txt


Task: Convert UNIX file to DOS format
Type the following command to convert file called myfile.txt:
$ unix2dos myfile.txt
$ unix2dos -b myfile.txt


Task: Convert Dos TO Unix Using tr Command
Type the following command:
tr -d '\r' < input.file > output.file

Task: Convert Dos TO Unix Using Perl One Liner
Type the following command:
perl -pi -e 's/\r\n/\n/g' input.file

Task: Convert UNIX to DOS format using sed command
Type the following command if you are using bash shell:
$ sed 's/$'"/`echo \\\r`/"   input.txt > output.txt
Note: sed version may not work under different UNIX/Linux variant,refer your local sed man page for more info.

Task: Convert DOS newlines (CR/LF) to Unix format using sed command
If  you are using BASH shell type the following command (press Ctrl-V then Ctrl-M to get pattern or special symbol)
$ sed 's/^M$//'  input.txt > output.txt
Note: sed version may not work under different UNIX/Linux variant, refer your local sed man page for more info.

http://www.cyberciti.biz/faq/howto-unix-linux-convert-dos-newlines-cr-lf-unix-text-format/


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-13 14:55 , Processed in 0.012964 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表