简体   繁体   中英

Trying to replace \r\n\n but not \r\n in a file

This is using GNU sed version 4.2.1 but I've also tried awk and Perl without any success so far.

I have a file that is produced by a COBOL program (on Linux) and it has what can be considered nonstandard CRLF instead of LF (CRLF of course being Windows line terminators) but that's what I need to retain - anything CRLF stays.

So \\r\\n sequences stay.

What I need to replace are occasional \\r\\n\\n sequences with \\r\\n\\r\\n without disturbing anything else.

I have to match this file I produce using diff with the original file produced on BSD or SCO or something.

This doesn't work and I expect the first /n is getting stripped by Sed as the line terminator

sed -e 's/\r\n\n/\r\n\r\n/g'  infile  > outfile

I tried hex 0x and also double escape too

Thanks for any suggestions

I suggest you just add a CR before any LF that isn't already preceded by one.

s/ (?<!\r) (?=\n) /\r/xg

In a program that alters the data in a file it would look something like this

use strict;
use warnings;

use open IO => ':raw';

my $data = do {
  local $/;
  <>;
};

$data =~ s/ (?<!\r) (?=\n) /\r/xg;

print $data;

and you would run it like

perl add_cr.pl myfile > newfile

or, if you wanted to modify your file in-place (after testing it) you could use just

perl -i add_cr.pl myfile

sed being a line oriented tool, blah\\r\\n\\n will be a line blah\\r followed by an empty line. So, add a \\r to any empty line:

sed 's/^$/\r/' infile > outfile

Just use this Perl one-liner:

perl -pe "s/\R/\r\n/g" <input.txt >output.txt

Magic here is about \\R which matches any new-line combination accepted by Perl: \\n , \\r\\n or \\r alone. As far as I know, \\R is Perl-only - not supported by sed or awk .

对于多字符RS,使用GNU awk:

awk -v RS='\r\n\n' -v ORS='\r\n\r\n' '1' file

Try unix2dos utility: It handle all unix/dos/ and mixture of unix/dos cases. Note: dos2unix is also a good utility.

Overwrite:

unix2dos your-file

Create new file:

unix2dos < your-file > your-new-file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM