How to remove carriage returns in the middle of a

2019-05-31 07:39发布

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

3条回答
我命由我不由天
2楼-- · 2019-05-31 08:07

Sounds like the easiest solution might be to check your filetype before moving between unix and windows. dos2unix and unix2dos might be what you really need, instead of a regex.

I'm not sure what character ^M is supposed to be, but carriage return is \015 or \r. So, s/\r//g should suffice. Remember it also removes your last carriage return, if that is something you wish to preserve.

查看更多
做自己的国王
3楼-- · 2019-05-31 08:17

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.

查看更多
一夜七次
4楼-- · 2019-05-31 08:27
use strict;
use warnings;

my $a = "field1|field2|field3|fie^Mld4|fiel^Md5|field6^M";

$a =~ s/\^M(?!$)//g;

print $a;
查看更多
登录 后发表回答