Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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 Oct 16 '11 at 6:45
    
The file originates from windows :P –  user394 Oct 16 '11 at 22:09
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
    
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 Oct 15 '11 at 22:49
    
I tried perl -e "s/\S\r\n\S/,/g" -pi.save vIndexes.txt and it didn't work :P –  user394 Oct 15 '11 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 Oct 15 '11 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? –  frozenwithjoy Oct 15 '11 at 23:12
add comment

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}'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.