Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How can I remove 0 from first line and empty space from the start of the following line and join them

by BrowserUk (Pope)
on Oct 16, 2002 at 02:19 UTC ( #205608=note: print w/ replies, xml ) Need Help??


in reply to How can I remove 0 from first line and empty space from the start of the following line and join them

Adapting this to read from your file and write to a new file should be easy.

#! perl -sw use strict; while (<DATA>) { $_ .= <DATA>; s/^0\s*(.*)\n\s+(.*\n)/$1 $2/s; print; } __DATA__ 0 54000 4.52402E+00 5.37331E+00 -5.37331E+00 -6.52528E-04 2.04301E-01 -1.07277E-03 0 54001 7.12575E+01 4.88288E+01 -4.88288E+01 -2.20964E-03 9.34513E-01 -1.76121E-03 0 54002 3.55673E+00 9.22908E+00 -9.22908E+00 1.05358E-04 1.78918E-01 1.74279E-04 0 54003 2.71198E+00 5.04349E+00 -5.04349E+00 0 1.73257E+00 4.45074E+00 0 54004 2.23790E+00 3.46888E+00 -3.46888E+00 -1.95970E-05 5.09952E-02 -1.95518E-05 0 54005 4.14851E+00 1.99183E+01 -1.99183E+01 -7.59083E-05 2.54499E-01 -1.53444E-04

# Output

C:\test>205602 54000 4.52402E+00 5.37331E+00 -5.37331E+00 -6.52528E-04 2.04301E-0 +1 -1.07277E-03 54001 7.12575E+01 4.88288E+01 -4.88288E+01 -2.20964E-03 9.34513E-0 +1 -1.76121E-03 54002 3.55673E+00 9.22908E+00 -9.22908E+00 1.05358E-04 1.78918E-01 + 1.74279E-04 54003 2.71198E+00 5.04349E+00 -5.04349E+00 0 1.73257E+00 4.45074E ++00 54004 2.23790E+00 3.46888E+00 -3.46888E+00 -1.95970E-05 5.09952E-0 +2 -1.95518E-05 54005 4.14851E+00 1.99183E+01 -1.99183E+01 -7.59083E-05 2.54499E-0 +1 -1.53444E-04 C:\test>

Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!


Comment on Re: How can I remove 0 from first line and empty space from the start of the following line and join them
Select or Download Code
Re: Re: How can I remove 0 from first line and empty space from the start of the following line and join them
by krusty (Hermit) on Oct 16, 2002 at 02:34 UTC
    An efficient alternative to:
    while (<DATA>) { $_ .= <DATA>; s/^0\s*(.*)\n\s+(.*\n)/$1 $2/s; print; }
    is not to even loop at all.
    { local $/ = undef; $_ = <DATA>; s/^0\s*(.*)\n\s+(.*\n)/$1 $2/s; print; }
    caveat is that it eats up memory if you have an extremely large file.

    Cheers,
    Kristina Update: See BrowserUK's post immediately below. /s would allow the "." to match the newline which I don't want to happen here, but /m will treat the single string as multiple lines and /g will let me match more than once before quitting.

      It would.... if you added /mg options instead of /s to the regex.



      Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-12-21 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (103 votes), past polls