如何在Bash脚本中将DOS / Windows换行符(CRLF)转换为Unix换行符(LF)?
本文翻译自:How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?
How can I programmatically (ie, not using vi
) convert DOS/Windows newlines to Unix? 如何以编程方式(即不使用vi
)将DOS / Windows换行符转换为Unix?
The dos2unix
and unix2dos
commands are not available on certain systems. 在某些系统上, dos2unix
和unix2dos
命令不可用。 How can I emulate these with commands like sed
/ awk
/ tr
? 如何使用sed
/ awk
/ tr
类的命令模拟这些?
#1楼
参考:https://stackoom.com/question/Axy4/如何在Bash脚本中将DOS-Windows换行符-CRLF-转换为Unix换行符-LF
#2楼
I tried sed 's/^M$//' file.txt on OSX as well as several other methods ( http://www.thingy-ma-jig.co.uk/blog/25-11-2010/fixing-dos-line-endings or http://hintsforums.macworld.com/archive/index.php/t-125.html ). 我尝试在OSX上使用sed's / ^ M $ //'file.txt以及其他几种方法( http://www.thingy-ma-jig.co.uk/blog/25-11-2010/fixing- dos-line-endings或http://hintsforums.macworld.com/archive/index.php/t-125.html )。 None worked, the file remained unchanged (btw Ctrl-v Enter was needed to reproduce ^M). 没有任何效果,文件保持不变(需要使用Ctrl-v Enter来重现^ M)。 In the end I used TextWrangler. 最后,我使用了TextWrangler。 Its not strictly command line but it works and it doesn't complain. 它不是严格的命令行,但是可以正常工作,并且不会抱怨。
#3楼
If you don't have access to dos2unix , but can read this page, then you can copy/paste dos2unix.py from here. 如果您没有访问dos2unix的权限,但可以阅读此页面,则可以从此处复制/粘贴dos2unix.py 。
#!/usr/bin/env python
"""\
convert dos linefeeds (crlf) to unix (lf)
usage: dos2unix.py <input> <output>
"""
import sysif len(sys.argv[1:]) != 2:sys.exit(__doc__)content = ''
outsize = 0
with open(sys.argv[1], 'rb') as infile:content = infile.read()
with open(sys.argv[2], 'wb') as output:for line in content.splitlines():outsize += len(line) + 1output.write(line + '\n')print("Done. Saved %s bytes." % (len(content)-outsize))
Cross-posted from superuser . 从超级用户交叉发布。
#4楼
Doing this with POSIX is tricky: 使用POSIX这样做很棘手:
POSIX Sed does not support
\\r
or\\15
. POSIX Sed不支持\\r
或\\15
。 Even if it did, the in place option-i
is not POSIX 即使这样做,就位选项-i
也不是POSIXPOSIX Awk does support
\\r
and\\15
, however the-i inplace
option is not POSIX POSIX Awk确实支持\\r
和\\15
,但是-i inplace
选项不是POSIXd2u and dos2unix are not POSIX utilities , but ex is d2u和dos2unix不是POSIX实用程序 ,但ex是
POSIX ex does not support
\\r
,\\15
,\\n
or\\12
POSIX ex不支持\\r
,\\15
,\\n
或\\12
To remove carriage returns: 要删除回车,请执行以下操作:
ex -bsc '%!awk "{sub(/\r/,\"\")}1"' -cx file
To add carriage returns: 要添加回车符:
ex -bsc '%!awk "{sub(/$/,\"\r\")}1"' -cx file
#5楼
For Mac osx if you have homebrew installed [ http://brew.sh/][1] 对于Mac osx,如果您安装了自制软件[ http://brew.sh/][1]
brew install dos2unixfor csv in *.csv; do dos2unix -c mac ${csv}; done;
Make sure you have made copies of the files, as this command will modify the files in place. 确保已复制文件,因为此命令将在适当位置修改文件。 The -c mac option makes the switch to be compatible with osx. -c mac选项使该开关与osx兼容。
#6楼
An even simpler awk solution w/oa program: 带有程序的更简单的awk解决方案:
awk -v ORS='\r\n' '1' unix.txt > dos.txt
Technically '1' is your program, b/c awk requires one when given option. 从技术上讲,“ 1”是您的程序,给定选项时,b / c awk需要一个。
UPDATE : After revisiting this page for the first time in a long time I realized that no one has yet posted an internal solution, so here is one: 更新 :在很长一段时间以来第一次重新访问此页面后,我意识到还没有人发布内部解决方案,所以这里是一个:
while IFS= read -r line;
do printf '%s\n' "${line%$'\r'}";
done < dos.txt > unix.txt
如何在Bash脚本中将DOS / Windows换行符(CRLF)转换为Unix换行符(LF)?相关推荐
- 如何在Bash脚本中将Heredoc写入文件?
如何在Bash脚本中将Here文档写入文件? #1楼 代替使用cat和I / O重定向,可以使用tee代替: tee newfile <<EOF line 1 line 2 line 3 ...
- 如何在bash脚本中提示用户进行确认? [重复]
本文翻译自:How do I prompt a user for confirmation in bash script? [duplicate] This question already has ...
- 把Windows换行符替换成UNIX换行符
1把Windows换行符替换成UNIX换行符: 去掉windows下的回车符 (注意^M 在linux 下写法 按^M 是回车换行符,输入方法是按住CTRL+v,松开v,按m) sed -i 's/ ...
- windows换行符linux替换,把Windows换行符替换成UNIX换行符
1把Windows换行符替换成UNIX换行符: 去掉windows下的回车符 (注意^M 在linux 下写法 按^M 是回车换行符,输入方法是按住CTRL+v,松开v,按m) sed -i 's/ ...
- linux脚本里用expect,如何在bash脚本中使用expect
这是我在 following bash脚本中使用的代码片段: for user_input in `awk '{print}' testfile_$$.txt` do ipaddress=`echo ...
- linux 杀死脚本,linux – 如何在Bash脚本被杀死时杀死当前命令
我能够在评论中测试我发布的prctl想法,它似乎有效.你需要编译这个: #include "sys/prctl.h" #include "stdlib.h" # ...
- shell bash脚本_如何在Windows 10上创建和运行Bash Shell脚本
shell bash脚本 With the arrival of Windows 10's Bash shell, you can now create and run Bash shell scri ...
- 如何利用python在一个文档里xie'ru_如何用python在Windows系统下,生成UNIX格式文件...
平时测试工做中,少不了制造测试数据.最近一个项目,我就须要制造一批可在UNIX下正确读取的文件.为确保这批文件能从FTP下载成功,开发叮嘱我:"文件中凡是遇到换行,换行符必须是UNIX下的L ...
- python将字符串s和换行符写入文件fp_软件测试技术之如何用python在Windows系统下,生成UNIX格式文件...
本文将带你了解软件测试技术之如何用python在Windows系统下,生成UNIX格式文件,希望对大家学测试技术有所帮助 如何用python在Windows系统下,生成UNIX格式文件 平时测试工作中 ...
最新文章
- 三层登录VB.NET实现
- UNIX再学习 -- 发送信号
- 看美女有助于男士长寿
- vue3.x案例 购物车
- MYSQL数据同步到ES7
- 4.namespace
- centos7下搭建hadoop、hbase、hive、spark分布式系统架构
- 【Latex】数学公式排版
- 多元线性回归模型中多重共线性问题处理方法
- getopt两个模块getopt 和gun_getopt 的异同
- 大整数减法的c语言程序,求用C编个大数加减法运算程序
- linux磁盘管理-vmware workstation模拟共享存储。
- python 矩阵元素平方_NumPy之计算两个矩阵的成对平方欧氏距离
- Gym - 101964E -Fishermen(二分+差分求前缀和)
- 消息称GPhone今日发布 揭秘幕后教父(图)
- .loc 与.iloc
- Pycharm提示No Python interpreter selected怎么解决
- Linux服务器如何查看CPU占用率、内存占用、带宽占用
- 设计一款可视化记录摩尔斯电码解码器,可以通过音频识别不同速度的摩尔斯电码
- 物联网--思科模拟器--室内家用电器控制
热门文章
- 受益匪浅:十个哲理寓言,十个成功秘诀
- 生日QQ配对【找到你生日QQ了吗?】
- ScrollView 嵌套EditText 滑动冲突解决
- android 使用广播监听网络状态
- python 提取网关信息_python:使用netifaces模块获取本机IP网关等信息
- Android之EditText的各种使用
- iOS架构-静态库.a之依赖第三方静态库.a的制作(8)
- swift_005(Swift的Dictionary 字典)
- 传输层端口号的范围是多少?被分为哪两部分_Cu2ZnSnS4纳米晶做空穴传输层经配体改性将低温可喷涂碳电极基钙钛矿太阳能电池的性能提高到17.71...
- uniapp中easycom组件的封装