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

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:
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:
DOG Not Found DOG Not found DOG DOG DOG DOG
Br, Pete

Replies are listed 'Best First'.
Re: Replacing a search/not found pattern with a word
by BrowserUk (Patriarch) on Mar 23, 2008 at 04:23 UTC
      Simpler:
      perl -nle"print /(DOG)/ ? $1 : 'Not found'"
      Or if you wanted to keep the -p:
      perl -ple"$_ = /(DOG)/ ? $1 : 'Not found'"

        I can shave a couple of strokes off of that:

        perl -lpe'$_=/DOG/?$&:"Not found"'

Re: Replacing a search/not found pattern with a word
by apl (Monsignor) on Mar 23, 2008 at 12:59 UTC
    You got some great answers, but I'd rather ask you: What did you try? Did you look at the RegEx tutorial?
      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:
      #! c:\perl\bin\perl use Net::Telnet; @look=$net->cmd("cat $file | grep -i 'DOG' | awk 'NR==1' | nawk '{prin +t \$2}'); for $find (@look) { $find =~ s/(^DOG)/\uNot Found!/g; print "$find\n"; }
      I intentionally replaced my standard input file due work related terminologies. =) Tnx so much indeed for your ideas and tips. Br,