Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Remove newlines and join lines

by hewarn (Initiate)
on Aug 28, 2002 at 13:44 UTC ( #193451=perlquestion: print w/replies, xml ) Need Help??

hewarn has asked for the wisdom of the Perl Monks concerning the following question:

Hi, If anyone has a elegant solution for this, please share it with me..

I am reading result a file and have already found the lines I wish to join but...this is not working....

I have names like Mary,Alan,David and I am searching for those from file. Once I have found it, I just would like to remove empty lines and attach the next written line after the word.

Original file looked like

Alan 2.13 Mary 2.16 And the new file would be: Alan 2.13 Mary 2.16
I cant seem to get that end of line off and join those lines...

Small but irritating problems cause I dont know how.

open (ROWS, "style.txt") || die "File not found"; while (defined ($rivi = <ROWS>)) { if($rivi =~ /^$names[$i]/) { chomp $rivi; $rivi=~tr/\n\n//; $rivi=~s/\n\n//; $rivi=~tr/0xb6//; $rivi=~tr/0xb6//; . . . . my $text=$text.$rivi; open (E,">>style1.txt"); print E $text; close E; }
BR Hewarn

Replies are listed 'Best First'.
Re: Remove newlines and join lines
by Django (Pilgrim) on Aug 28, 2002 at 14:43 UTC
    Indeed there are many ways... so here's mine:
    #!usr/bin/perl open ROWS, "< style.txt" or die "Can't read from style.txt"; undef $/; # no input record separator $_ = <ROWS>; # copy the whole file to default var close ROWS; s/\s*(\w+)[\n\s]+([-.\d]+)[\n\s]+/$1 $2\n/g; # substitute your described input format # with requested output format in the whole text (now $_) open E, "> style1.txt" or die "Can't write in style1.txt"; print E; close E;
Re: Remove newlines and join lines
by cfreak (Chaplain) on Aug 28, 2002 at 14:02 UTC

    You're on the right track. What's happening is that when you open something the way you have it uses 'EOF' as the record. 'EOF' is usually after the newline character so it will leave you with one '\n' each time through the loop, thus your pattern trying to match '\n\n' fails. You really don't need a regex at all, all you need to do is chomp. If you only have one newline on a particular line the line will be deleted because the '\n' character is all that's there. This should work:

    while(defined($rivi = <ROWS>)) { chomp $rivi; .... }

    Now chomp is removing all the '\n' characters so if you want each name on a separate line you'll need to add it back when you write to the file:

    open(E,">>style1.txt") or die $!; print E "$rivi\n"; # not understanding why you have $text here close(E);

    That will append the $rivi and a newline. Unless you have some other info in $text that you defined before this block of code I don't think you really need it (if you want the output you showed above)

    I can think of two other ways to do this, if you're interested in those please reply

    Chris

    Lobster Aliens Are attacking the world!
Re: Remove newlines and join lines
by moxliukas (Curate) on Aug 28, 2002 at 14:14 UTC

    Here is a way that I used to solve your problem (be careful about the number of newlines and alike. It is a very fragile approach)

    #!/usr/bin/perl -w use strict; my $a = join '', <DATA>; $a =~ s/\n{2}(\d.\d+)/ $1/g; $a =~ s/\n{2}/\n/g; print $a; __DATA__ Alan 2.13 Mary 2.16

    This prints:

    Alan 2.13 Mary 2.16

    I hope this is of any use.

Re: Remove newlines and join lines
by mce (Curate) on Aug 28, 2002 at 14:21 UTC
    Aha,

    One of these there-are-many-ways-to-solve-this perl questions.

    { local($/)=undef; map { s/([A-Za-z]+)\W+([\d\.]+)/print STDOUT "$1 $2\n"/ge } <DATA>; } exit 0; __DATA__ Alan 2.13 Mary 2.16

    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium
Re: Remove newlines and join lines
by Abigail-II (Bishop) on Aug 28, 2002 at 14:54 UTC
    Something like:
    perl -pi -we 'chomp; s/^([A-Za-z])/$1 /; s/^([\d.]+)/$1\n/' style.t +xt
    Abigail
Re: Remove newlines and join lines
by hotshot (Prior) on Aug 28, 2002 at 13:50 UTC
    just read the file to an array, then loop on the array. while the line don't match your names, push it to a new array, when the line matches your name push it too to the new array and skip the empty line.

    Hotshot
Re: Remove newlines and join lines
by hewarn (Initiate) on Aug 28, 2002 at 15:44 UTC
    Hi, it seems indeed many solutions exist, I tried some of then and will continue to test.

    Finally the "chomp" solution was easy to put in my code as I already had it and removed all unnesseray stuff. But what about if the next line contains data I wish also to have left also?

    Like original file

    Mary 2.12 Some text here we want too David 3.13 Even more text here
    This is what we wish to have:
    Mary 2.12 Some text here we want too David 3.13 Even more text here
    BR Hewarn

      Effectively what you want to do is remove any blank lines and then join every second remaining to it's predecessor. So... (untested)

      #open INPUT and OUTPUT while (<INPUT>) { chomp; next if m/^\s+$/; my $nextline; chomp($nextline = <INPUT> ) while $nextline !~ m/^\s+/; print OUTPUT $_,' ',$nextline; } #close INPUT and OUTPUT

      What's this about a "crooked mitre"? I'm good at woodwork!
      Replacing the regex in my first answer with that one should work:
      s/\s*(\w+)[\n\s]+([^\n]+)[\n\s]+/$1 $2\n/g;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2022-07-04 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?