How to delete the lines from text file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • priyankapendkar
    New Member
    • Apr 2011
    • 9

    How to delete the lines from text file?

    Hi

    i am having an text file

    test1.txt contains the following data:

    Code:
    Abcd
    2222
    ERROR:duplicate
    ERROR:Getinstrument
    ERROR:duplicate
    www
    xxx
    here i want the output which only contain the word error

    if i use the command :v/ERROR/d

    then output :
    Code:
    ERROR:duplicate
    ERROR:Getinstrument
    ERROR:duplicate
    now i want the output which contains the line other than
    ERROR:duplicate

    output should be only ERROR:Getinstru ment

    can anybody help me in this, how to get the lines other than ERROR:duplicate ???

    Thanks,
    Priyanka
    Last edited by miller; Apr 13 '11, 06:05 PM.
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Use a hash to match and skip duplicate lines:

    perlfaq4 - How can I remove duplicate elements from a list or array?

    - Miller

    Comment

    • gaurav khurana
      New Member
      • Nov 2012
      • 1

      #3
      input
      *************
      Code:
      /home/user> cat inputfile
      Abcd
      2222
      ERROR:duplicate
      ERROR:Getinstrument
      ERROR:duplicate
      www
      xxx
      /home/user>
      program
      *************
      Code:
      /home/user> cat > prog
      open(FH,"inputfile");
      open(FH1,">","outputfile");
      
      while(<FH>)
      {
       if ( $_ =~ m/ERROR/)
       {
         if( $_ =~ m/duplicate/)
         {
          ## When Error & duplicate are there in the input we are not writing it to output file
         }
         else
         {
         ##prinitng those lines which are not having error or duplicate
         print FH1 $_;
         }
       }
      }
      close(FH);
      close(FH1);
      running program
      ********
      Code:
      /home/user> perl prog
      output
      *******
      Code:
      /home/user> cat outputfile
      ERROR:Getinstrument
      /home/user>
      hope this can work
      Last edited by Rabbit; Nov 21 '12, 04:27 PM. Reason: Please use code tags when posting code.

      Comment

      Working...