Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to remove extra line breaks (using regexp)?

by larus (Acolyte)
on Apr 17, 2009 at 14:03 UTC ( #758238=perlquestion: print w/ replies, xml ) Need Help??
larus has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Basic question, probably asked already thousand times, but I could not find a solution for this. I have a file with extra line breaks
A6.8.5 This is an example sentence A6.8.6 This is an example sentence
How can I remove those extra line breaks and get each sentence back to original form:
A6.8.5 This is a example sentence A6.8.6 This is a example sentence
I have tried something like this:
..snip.. $line =~ s/\r(?!\n)/ /g;

Comment on How to remove extra line breaks (using regexp)?
Select or Download Code
Re: How to remove extra line breaks (using regexp)?
by ikegami (Pope) on Apr 17, 2009 at 14:07 UTC
    How can you tell which line break to keep so it doesn't turn into
    A6.8.5 This is a example sentence A6.8.6 This is a example sentence
    Will the line always start with /A\d+\./?
      Well, yeah, that is the problem. There is other shitty thing also in this .txt file that I'm trying to clean but basically the lines that I want to keep starts with a letter and a digit.
        my $file = do { local $/; <DATA> }; $file =~ s/\n(?!A\d+\.)/ /g; $file .= "\n"; print $file; __DATA__ A6.8.5 This is an example sentence A6.8.6 This is an example sentence
        A6.8.5 This is an example sentence A6.8.6 This is an example sentence
Re: How to remove extra line breaks (using regexp)?
by bichonfrise74 (Vicar) on Apr 18, 2009 at 05:18 UTC
    Would this work?
    #!/usr/bin/perl use strict; my @file = <DATA>; foreach my $i (@file) { $i =~ s/\n/ /; $i =~ /^A\d\.\d\./ ? print "\n$i" : print $i; } __DATA__ A6.8.5 This is an example sentence A6.8.6 This is an example sentence
      yes, it works, thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://758238]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-09-30 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (368 votes), past polls