Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Delete strings

by sandy1028 (Sexton)
on Apr 10, 2009 at 07:48 UTC ( [id://756766]=perlquestion: print w/replies, xml ) Need Help??

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

How to delete the lines from the file until it finds the string '1.0'.
When it finds the string '1.0' It should npt delete any lines.

Replies are listed 'Best First'.
Re: Delete strings
by almut (Canon) on Apr 10, 2009 at 08:09 UTC
    perl -i -ne 'print if $end_del ||= /1\.0/' file

    /1\.0/ does a substring match; if you want to stop deleting only when the line contains nothing but "1.0", either use /^1\.0$/, or $_ eq "1.0\n".

      Another way to do that:

      perl -i -ne 'print if /1\.0/ .. eof' file
      What's with the idea you have to write the solution in a single line of code?
      This can be done easily with a few lines of code, why write it in a single line? Are you running with 8MB of ram or something?
      1. Open the file IO in read write mode.
      2. Read the file into an array.
      3. Set up a good old fashioned boolean $foundString
      4. Traverse the array til the boolean is true printing stuff to the file.
      5. Boolean is true close the file..DONE.
        ... why write it in a single line?

        Why not?  I never said you have to... Feel free to write as many lines as makes you feel comfortable.  I just happen to like one-liners for tasks like these — which doesn't imply anyone else would have to like or use them, too.  Yet others like golfing. Just for the fun of it...

Re: Delete strings
by ikegami (Patriarch) on Apr 10, 2009 at 08:13 UTC

    You can't delete lines from a file. You can copy a file while doing modifications to what is copied. So after opening the two files,

    1. While the source hasn't been exhausted,
      1. Read a line from the source.
      2. If line contains '1.0' or such a line was already found,
        1. Print the line to the destination.

    As a one-liner:

    perl -ne"print if $f ||= /1\.0/" infile >outfile

    or in-place:

    perl -i.bak -ne"print if $f ||= /1\.0/" file

    Update: The above were for the Windows Command shell. Below are adaptations for bash:

    perl -ne'print if $f ||= /1\.0/' infile >outfile

    or in-place:

    perl -i~ -ne'print if $f ||= /1\.0/' file

      Your code is not working for me!

      Vinoth,G

        Try executing as below

        perl -i.bak -ne"print if "\$f" ||= /1\.0/" file

        In unix the shell interprets the $f. So you were unable to execute.

        --Lakshmanan G.

        The great pleasure in my life is doing what people say you cannot do.


        Are you on Unix or on Windows? On Unix, the shell would interpolate $f in double quoted strings, so it's usually easier to use single quotes for Perl one-liners...  OTOH, on Windows you'd have to use double quotes, as single quotes don't work (as quotes) with the standard shell...

        ree: your reply is not an error message for me.

        Update: identified node to which this is a response.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found