Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by Anonymous Monk
on Oct 16, 2002 at 02:04 UTC ( [id://205602]=perlquestion: print w/replies, xml ) Need Help??

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

I've been trying to remove zero from the first line and empty space from the start of the following line and join them together and print them to a new file.
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
Desired result:
54000 4.52402E+00 5.37331E+00 -5.37331E+00 -6.52528E-04 2.04301 +E-01 -1.07277E-03 54001 7.12575E+01 4.88288E+01 -4.88288E+01 -2.20964E-03 9.34513 +E-01 -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.45 +074E+00 54004 2.23790E+00 3.46888E+00 -3.46888E+00 -1.95970E-05 5.09952 +E-02 -1.95518E-05 54005 4.14851E+00 1.99183E+01 -1.99183E+01 -7.59083E-05 2.54499 +E-01 -1.53444E-04
Can you help me?
  • Comment on 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

Replies are listed 'Best First'.
Re: How can I remove 0 from first line and empty space from the start of the following line and join them
by BrowserUk (Patriarch) on Oct 16, 2002 at 02:19 UTC

    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!
      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!
Re: How can I remove 0 from first line and empty space from the start of the following line and join them
by sauoq (Abbot) on Oct 16, 2002 at 02:34 UTC
    perl -i.bak -n0e 's/0\s+(.*)\n\s+(.*)/$1 $2/g&&print' file

    This, a combination of BrowserUk's correct method and Enlil's short method, should do what you want in one line.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: How can I remove 0 from first line and empty space from the start of the following line and join them
by arturo (Vicar) on Oct 16, 2002 at 02:28 UTC

    Basic idea:

    1. read in two-line chunks (read one line, then another)
    2. Join the lines.
    3. Strip out the leading zero, and any excess whitespace (including newlines).

    open FILE, "file.txt" or die "Can't open file.txt:$!\n"; while(my $firstline = <FILE>) { $secondline = <FILE> or die "Ran out of lines!\n"; # how to join the lines is left as an exercise for the # reader: hint ... Dot knows much. }

    For the second part, you can use a regular expression. Here's a hint: /^a/ matches an a at the beginning of a string. Here's another hint: \s{7,} matches seven whitespace (space, tab, newline) characters. The general syntax for a search-and-replace (substitution) pattern match is $string =~ s/tick/arthur/;, and the /g modifier on the end of a regular expression tells the system to perform each match it finds (instead of just the first).

    The rest is left as an exercise for the reader. Study materials: perlre, perlretut.

    HTH

    Update theorbtwo pointed out I hadn't closed my ordered list tag.

    If not P, what? Q maybe?
    "Sidney Morgenbesser"

Re: How can I remove 0 from first line and empty space from the start of the following line and join them
by Enlil (Parson) on Oct 16, 2002 at 02:17 UTC
    try this:
    perl -pi.bak -e "s/^0\s+//;" file_name
    This will look for a 0 at the start of every line followed by spaces and remove them (if this pattern does not exist it leaves the line alone), and then print the line back out to file you pass it the i.bak will create a backup file with the filename as file_name.bak. I believe this is what you are asking for.

    UPDATE: I neglected the second line part. My mistake. See the responses below for a better more complete answer.

    -Enlil

Re: How can I remove 0 from first line and empty space from the start of the following line and join them
by Aristotle (Chancellor) on Oct 16, 2002 at 14:16 UTC
    If you only need to do this once, vim will do the job in a blink of an eye: :%g/^0/norm 0dwJ Doncha just love that editor. :-)

    Makeshifts last the longest.

Re: How can I remove 0 from first line and empty space from the start of the following line and join them
by Anonymous Monk on Oct 16, 2002 at 14:09 UTC
    Thanks to all who responded. Perlmonks is great!
Re: How can I remove 0 from first line and empty space from the start of the following line and join them
by antifun (Sexton) on Oct 16, 2002 at 15:19 UTC
    Since a vim solution was posted, here's an awk one:
    awk 'BEGIN { ORS=""; OFS="\t" } /^0/ {print $2, $3, $4, $5} /^[ \t]/ +{print $0, "\n" }'
    TMTOWTDI, even if it's not in perl :)
    ---
    "I hate it when I think myself into a corner."
    Matt Mitchell

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-20 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found