Convert between windows text file to Unix type

简介:

dos2unix and unix2dos

The utilities dos2unix and unix2dos are available for converting files from the Unix command line.

To convert a Windows file to a Unix file, enter:

dos2unix winfile.txt unixfile.txt

To convert a Unix file to Windows, enter:

unix2dos unixfile.txt winfile.txt

tr

You can use tr to remove all carriage returns and Ctrl-z ( ^Z ) characters from a Windows file:

tr -d '\15\32' < winfile.txt > unixfile.txt

However, you cannot use tr to convert a document from Unix format to Windows.

awk

To use awk to convert a Windows file to Unix, enter:

awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt

To convert a Unix file to Windows, enter:

awk 'sub("$", "\r")' unixfile.txt > winfile.txt

Older versions of awk do not include the sub function. In such cases, use the same command, but replace awk with gawk or nawk.

Perl

To convert a Windows text file to a Unix text file using Perl, enter:

perl -p -e 's/\r$//' < winfile.txt > unixfile.txt

To convert from a Unix text file to a Windows text file, enter:

perl -p -e 's/\n/\r\n/' < unixfile.txt > winfile.txt

You must use single quotation marks in either command line. This prevents your shell from trying to evaluate anything inside.

vi

In vi, you can remove carriage return ( ^M ) characters with the following command:

:1,$s/^M//g

Note: To input the ^M character, press Ctrl-v , and then press Enter or return.

In vim, use :set ff=unix to convert to Unix; use :set ff=dos to convert to Windows. 

目录
相关文章
|
存储 Linux Windows
【应用服务 App Service】App Service For Windows 如何挂载Storage Account File Share 示例
【应用服务 App Service】App Service For Windows 如何挂载Storage Account File Share 示例
120 0
|
Linux Python Windows
【Azure 环境】Windows中安装Python azure-eventhub-checkpointstoreblob-aio模块时出错 ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory:
【Azure 环境】Windows中安装Python azure-eventhub-checkpointstoreblob-aio模块时出错 ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory:
122 0
|
存储 文件存储 数据安全/隐私保护
Windows部署开源文件管理器File Browser并实现远程访问本地文件
Windows部署开源文件管理器File Browser并实现远程访问本地文件
824 1
|
Unix Linux Shell
【文件格式问题】文件格式 Windows、Unix/Linux、Mac 导致的问题及处理(idea 或 notepad++ 档案格式转换方法)
【文件格式问题】文件格式 Windows、Unix/Linux、Mac 导致的问题及处理(idea 或 notepad++ 档案格式转换方法)
312 0
|
移动开发 Unix Linux
一文搞清UNIX/Linux与Windows文件换行符格式差异
一文搞清UNIX/Linux与Windows文件换行符格式差异
857 0
一文搞清UNIX/Linux与Windows文件换行符格式差异
|
Windows
Cannot open include file: ‘Windows.h‘: No such file or directory
Cannot open include file: ‘Windows.h‘: No such file or directory
315 0
|
C++ Windows
【错误记录】Windows 控制台程序编译报错 ( fatal error C1083: 无法打开包括文件: “afxwin.h”: No such file or directory )
【错误记录】Windows 控制台程序编译报错 ( fatal error C1083: 无法打开包括文件: “afxwin.h”: No such file or directory )
1630 0
【错误记录】Windows 控制台程序编译报错 ( fatal error C1083: 无法打开包括文件: “afxwin.h”: No such file or directory )
|
监控 Unix Linux
LoadRunner监控Unix、Windows方法及常用性能指标
目  录 一、LoadRunner监控Linux资源.... 3 (一)、准备工作... 3 1、可以通过两种方法验证服务器上是否配置了rstatd守护程序:... 3 (2)使用find命令.
2133 0
|
Unix Shell API
windows与unix思想
Unix与Windows的思想 Unix中的哲学是“一切皆文件”,这里的一切皆文件是一个广泛的概念,有一些特殊的设备文件,在/dev目录下 物理设备在Unix中就对应一个特殊的设备文件,比如打印机就是/dev/lp0,这个设备文件直接与物理设备的串行端口连接,只要向这个设备文件中传入数据,就可以调用打印机。
1175 0