Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Delete strings

by ikegami (Patriarch)
on Apr 10, 2009 at 08:13 UTC ( #756769=note: print w/replies, xml ) Need Help??

in reply to Delete strings

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

Replies are listed 'Best First'.
Re^2: Delete strings
by vinoth.ree (Monsignor) on Apr 10, 2009 at 08:51 UTC

    Your code is not working for me!


      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...

        I am on Unix, Now I understand it. Thanks


      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.

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

      Update: identified node to which this is a response.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2023-11-30 03:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found