如何删除行中的回车 [英] How to remove carriage returns in the middle of a line

查看:122
本文介绍了如何删除行中的回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用程序在unix和windows中读取的文件。但是,当我在数据中间以^ M读取窗口时遇到问题。我只是想删除^ M,如字段4和字段5中间。

我已经尝试使用 perl - pe's / \cM\cJ?// g',但是它把所有东西都删除到了我不想要的一行中。我希望数据保持在同一行,但删除多余的数据

 #Comment ^ M 
#field1_header | field2_header | field3_header | field4_header | field5_header | field6_header ^ M
#^ M
field1 | field2 | field3 | fie ^ Mld4 | fiel ^ Md5 | field6 ^ M
^ M

谢谢 在行的中间删除CR:

  perl -pe's / \r(?!\\\
)// g'

你也可以写这个 perl -pe's / \cM(?!\cJ)// G'?!结构是负向预测表达式。该模式匹配一​​个CR,但只有当它没有跟随一个LF。



当然,如果使用unix换行符生成文件是可以接受的, CR字符:

  perl -pe'tr / \015 // d'

你写了什么, s / \cM\cJ?// g ,条一个CR和它后面的LF,如果有的话,因为LF是匹配模式的一部分。


I have file that is read by application in unix and windows. However I am encountering problems when reading in windows with ^M in the middle of the data. I am only wanting to remove the ^M in the middle of the lines such as field 4 and field 5.

I have tried using perl -pe 's/\cM\cJ?//g' but it removes everything into one line which i don't want. I want the data to stay in the same line but remove the extra ones

# Comment^M
# field1_header|field2_header|field3_header|field4_header|field5_header|field6_header^M
#^M
field1|field2|field3|fie^Mld4|fiel^Md5|field6^M
^M

Thanks

解决方案

To just remove CR in the middle of a line:

perl -pe 's/\r(?!\n)//g'

You can also write this perl -pe 's/\cM(?!\cJ)//g'. The ?! construct is a negative look-ahead expression. The pattern matches a CR, but only when it is not followed by a LF.

Of course, if producing a file with unix newlines is acceptable, you can simply strip all CR characters:

perl -pe 'tr/\015//d'

What you wrote, s/\cM\cJ?//g, strips a CR and the LF after it if there is one, because the LF is part of the matched pattern.

这篇关于如何删除行中的回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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