1

I have a text file of a database dump with some line break characters (0x0A0x0D) in the middle of lines. I want to replace them with commas, but I can't do it simply, because those characters are the actual line break characters where I do want line breaks!

But I noticed that the line break sequences I want to keep are surrouned by space characters ( 0x20), so I was thinking a regex to find and replace any 0x0A0x0D sequence without a leading or trailing space.

How can I do this?

2
  • I'm on android so I can't check this. I believe you can use sed and match \x0a\x0d after each other. In a unix file the line endings you want to keep are lf alone.
    – johnny
    Commented Oct 16, 2011 at 6:45
  • The file originates from windows :P
    – user394
    Commented Oct 16, 2011 at 22:09

2 Answers 2

1

The regex for a whitespace character is, of course, \s. However, since you want a non-whitespace character, you can use \S! Therefore, your regex to replace would be \S\n\r\S.

EDIT:

#!/usr/bin/perl
use strict; use warnings;
my $pattern = "xxxxxxxxxxxxxxxxxxxy\n\ryxxxxxxxxxxxxxxxxxxx \n\r xxxxxxxxxxxxxxxxxxxy\n\ryxxxxxxxxxxxxxxxxxxx";
$pattern =~ s/(\S)(\n\r)(\S)/$1$3/g;
print "$pattern\n";
exit;

result:

xxxxxxxxxxxxxxxxxxxyyxxxxxxxxxxxxxxxxxxx 

 xxxxxxxxxxxxxxxxxxxyyxxxxxxxxxxxxxxxxxxx

I changed the regex to replace with $1$3 so you retain the characters that \S matches.

4
  • Sorry to be confusing -- 0x0A0x0D I mean to be the hex notation for the \r\n dos-style line breaks. They aren't literal characters.
    – user394
    Commented Oct 15, 2011 at 22:49
  • I tried perl -e "s/\S\r\n\S/,/g" -pi.save vIndexes.txt and it didn't work :P
    – user394
    Commented Oct 15, 2011 at 22:52
  • Thinking that maybe I was dumb and the perl regex parser understood hex characters, I also tried perl -e "s/\S0x0A0x0D\S/replaced/g;" -pi.save testfile and it didn't work either.
    – user394
    Commented Oct 15, 2011 at 22:54
  • Are you looking for \r\n or \n\r? Try perl -e "s/\S\n\r\S/,/g" -pi.save vIndexes.txt instead? Commented Oct 15, 2011 at 23:12
0

Here's a way with GNU awk. Set the record separator RS to match the separators you want to keep, and weed out the other \r\n sequences.

gawk -vRS=' \r\n ' '{gsub(/\r\n/, ""); printf "%s%s", $0, RT}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .