在Linux上删除Windows换行符(SED与AWK) [英] Removing Windows newlines on Linux (sed vs. awk)

查看:514
本文介绍了在Linux上删除Windows换行符(SED与AWK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有放置不当换行符一些分隔的文件中的字段的中间(未行结束),显示为^ M Vim中。它们都来源于freebcp(在CentOS 6)MSSQL数据库的出口。倾倒在十六进制的数据显示\\ r \\ n模式:

Have some delimited files with improperly placed newline characters in the middle of fields (not line ends), appearing as ^M in Vim. They originate from freebcp (on Centos 6) exports of a MSSQL database. Dumping the data in hex shows \r\n patterns:

$ xxd test.txt | grep 0d0a
0000190: 3932 3139 322d 3239 3836 0d0a 0d0a 7c43

我可以使用awk删除它们,但我无法做同样的SED。

I can remove them with awk, but am unable to do the same with sed.

这工作在awk中,除去该行完全打破:

This works in awk, removing the line breaks completely:

awk 'gsub(/\r/,""){printf $0;next}{print}'

但是,这在sed不会,留下线到位提要:

But this in sed does not, leaving line feeds in place:

sed -i 's/\r//g'

在那里,这似乎没有任何效果:

where this appears to have no effect:

sed -i 's/\r\n//g'

在战略经济对话前pression(CTRL + V,Ctrl + M键)也似乎不工作中使用^ M。

Using ^M in the sed expression (ctrl+v, ctrl+m) also does not seem to work.

有关这种任务,sed的更容易神交,但我正在学习更多关于两者。我使用的sed不当,或者是有一个限制?

For this sort of task, sed is easier to grok, but I am working on learning more about both. Am I using sed improperly, or is there a limitation?

推荐答案

我相信 SED 的某些版本将无法识别 \\ r 为一个字符。但是,您可以使用庆典功能来解决这个限制:

I believe some versions of sed will not recognize \r as a character. However, you can use a bash feature to work around that limitation:

echo $string | sed $'s/\r//'

在这里,你让庆典替换'\\ r'里面实际的回车符 $...传递,为 SED 作为它的命令之前构建。 (假设你使用庆典;其他外壳应该有一个类似的结构)

Here, you let bash replace '\r' with the actual carriage return character inside the $'...' construct before passing that to sed as its command. (Assuming you use bash; other shells should have a similar construct.)

这篇关于在Linux上删除Windows换行符(SED与AWK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆