Reputation: 63
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
Upvotes: 1
Views: 316
Reputation: 4504
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
Upvotes: 0
Reputation: 203985
WIth GNU awk for multi-char RS:
awk -v RS='\r\n\n' -v ORS='\r\n\r\n' '1' file
Upvotes: 0
Reputation: 247012
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
Upvotes: 2
Reputation: 116317
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
.
Upvotes: 0
Reputation: 126742
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
Upvotes: 2