Replacing a search/not found pattern with a word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pinpe
    New Member
    • Aug 2007
    • 7

    Replacing a search/not found pattern with a word

    Hi,

    I want to search a pattern from each line of my print output with a word "DOG". And those lines without the word "DOG" I will replace the entire line with the word "Not Found". How can I do it? Pls advice. Tnx in advance.

    Input:
    Code:
    My DOG name 
    *** CAT is in the kitchen
    Name DOG
    *** CAT is in the kitchen
    My DOG name
    My DOG name
    My DOG name
    My DOG name
    
    print $animals | grep 'DOG' | nawk '{print $2}'
    Desired Output:
    Code:
    DOG
    Not Found
    DOG
    Not found
    DOG
    DOG
    DOG
    DOG

    Br,
    Pete
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by pinpe
    Hi,

    I want to search a pattern from each line of my print output with a word "DOG". And those lines without the word "DOG" I will replace the entire line with the word "Not Found". How can I do it? Pls advice. Tnx in advance.

    Input:
    Code:
    My DOG name 
    *** CAT is in the kitchen
    Name DOG
    *** CAT is in the kitchen
    My DOG name
    My DOG name
    My DOG name
    My DOG name
    
    print $animals | grep 'DOG' | nawk '{print $2}'
    Desired Output:
    Code:
    DOG
    Not Found
    DOG
    Not found
    DOG
    DOG
    DOG
    DOG

    Br,
    Pete
    What have you tried thus far to do this? Please post your code and we will help you from there.

    Regards,

    Jeff

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      He posted this question on at least 3 other forums. Likely that there was not an attempt made.

      --Kevin

      Comment

      • pinpe
        New Member
        • Aug 2007
        • 7

        #4
        Hi,

        My apology of giving incomplete info. These were the commands I tried. Actually I want to search from every line of my standard output file the word "DOG" and if not found replaced the entire line with the word "Not Found!". I was confused if I will use a conditional statements or simply using search & replace method. And I end up with the solution below:

        Code:
        #! c:\perl\bin\perl
        
        use Net::Telnet;
        
        @look=$net->cmd("cat $file | grep -i 'DOG' | awk 'NR==1' | nawk '{print \$2}');
        for $find (@look) {
        [b]$find =~ s/(^DOG)/\uNot Found!/g;[/b]
        print "$find\n"; 
        }
        I intentionally replaced my standard input file due work related terminologies. =)

        Tnx so much indeed for your ideas and tips.

        Br,

        Comment

        • dvijayan
          New Member
          • Mar 2008
          • 3

          #5
          [CODE=perl]open (IN, "lines.txt" );
          @words = <IN>;
          foreach my $m (@words)
          {
          if ($m=~/DOG/)
          {
          $m=~s/.*DOG.*/DOG/g;
          }
          else
          {
          $m=~s/^.*$/NOT FOUND/g;
          }

          print $m;

          }[/CODE]
          Last edited by eWish; Mar 27 '08, 01:01 PM. Reason: Please use code tags

          Comment

          Working...