Loading ...
Sorry, an error occurred while loading the content.
 

How to remove unwanted lines from file.

Expand Messages
  • Ramesh Polepalli
    Hi, I want to remove some lines containing some specific characters in a file. I can easily do it using grep redirecting the output to some other file and
    Message 1 of 4 , May 10, 2007
      Hi,

      I want to remove some lines containing some specific characters in a
      file.



      I can easily do it using grep redirecting the output to some other file
      and renaming the new file with source file name



      In Shell

      ---------------

      cat Sourcefile | grep -v "ignorestring" > newfile

      mv newfile Sourcefile



      Is there any command line "One liner" available to do the same in Perl
      without having to create new file and renaming it back?



      Thanks in advance for any help.





      Ramesh Polepalli









      mFormation Technologies Inc.
      Direct Line: +91 80 6620 5939
      Switchboard: +91 80 6620 5900

      http://www.mformation.com










      Visit WWW.MFORMATION.COM and see how the MFORMATION SERVICE MANAGER can help you to transform your business.

      [Non-text portions of this message have been removed]
    • merlyn@stonehenge.com
      ... Ramesh cat Sourcefile | grep -v ignorestring newfile That s a useless use of cat - grep for that for details. Ramesh mv newfile Sourcefile Ramesh
      Message 2 of 4 , May 10, 2007
        >>>>> "Ramesh" == Ramesh Polepalli <ramesh.polepalli@...> writes:

        Ramesh> cat Sourcefile | grep -v "ignorestring" > newfile

        That's a "useless use of cat" - grep for that for details.

        Ramesh> mv newfile Sourcefile

        Ramesh> Is there any command line "One liner" available to do the same in Perl
        Ramesh> without having to create new file and renaming it back?

        Yes, it's called "in-place editing", and is described on "perldoc perlrun"
        under the "-i" switch. If you have any questions after reading that
        document, please ask again.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      • Jenda Krynicky
        From: Ramesh Polepalli ... perl -pi.bak -e $_ = if /ignorestring/ sourcefile ===== Jenda@Krynicky.cz ===
        Message 3 of 4 , May 10, 2007
          From: "Ramesh Polepalli" <ramesh.polepalli@...>
          > I want to remove some lines containing some specific characters in a
          > file.
          >
          > Is there any command line "One liner" available to do the same in Perl
          > without having to create new file and renaming it back?

          perl -pi.bak -e "$_ = '' if /ignorestring/" sourcefile
          ===== Jenda@... === http://Jenda.Krynicky.cz =====
          When it comes to wine, women and song, wizards are allowed
          to get drunk and croon as much as they like.
          -- Terry Pratchett in Sourcery
        • merlyn@stonehenge.com
          ... Jenda perl -pi.bak -e $_ = if /ignorestring/ sourcefile I find perl -ni.bak -e print unless /ignorestring/ sourcefile saner to read. Somehow, the
          Message 4 of 4 , May 10, 2007
            >>>>> "Jenda" == Jenda Krynicky <Jenda@...> writes:

            Jenda> perl -pi.bak -e "$_ = '' if /ignorestring/" sourcefile

            I find

            perl -ni.bak -e 'print unless /ignorestring/" sourcefile

            saner to read. Somehow, the idea of "print ''" seems wasteful,
            and potentially dangerous if "-l" is involved.

            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
            See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
          Your message has been successfully submitted and would be delivered to recipients shortly.