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

How to delete the entire line if it contains the word "IT" in afile.

Replies are listed 'Best First'.
Re: Delete line
by CountZero (Bishop) on Aug 14, 2009 at 05:43 UTC
    What have you tried?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Delete line
by jwkrahn (Abbot) on Aug 14, 2009 at 05:50 UTC
    $line = '' if index( $line, 'IT' ) >= 0;
      #!/usr/bin/perl while(<DATA>){ @contents = grep { !/IT/ } $_; print @contents; } __DATA__ Economic slow down in IT HI
      I have tried this. But I dont want to store and print an array. While reading the lines, if it contains the word "IT" I have to delete the line
        When you say the word "IT" do you really mean "IT" as a word? It is important:
        use strict; use warnings; while(<DATA>) { print if /IT/ } __DATA__ This is IT for now Nothing of interest here Looping is used for ITeration
        gives:
        This is IT for now Looping is used for ITeration
        However if we use the word boundary anchor \b:
        while(<DATA>) { print if /\bIT\b/ }
        we only get
        This is IT for now
Re: Delete line
by Bloodnok (Vicar) on Aug 14, 2009 at 11:58 UTC
    perl -ne 'print unless /IT/' afile
    A user level that continues to overstate my experience :-))