By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Replacing a search/not found pattern with a word

P: 7
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:
Expand|Select|Wrap|Line Numbers
  1. My DOG name 
  2. *** CAT is in the kitchen
  3. Name DOG
  4. *** CAT is in the kitchen
  5. My DOG name
  6. My DOG name
  7. My DOG name
  8. My DOG name
  9.  
  10. print $animals | grep 'DOG' | nawk '{print $2}'
Desired Output:
Expand|Select|Wrap|Line Numbers
  1. DOG
  2. Not Found
  3. DOG
  4. Not found
  5. DOG
  6. DOG
  7. DOG
  8. DOG

Br,
Pete
Mar 23 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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:
Expand|Select|Wrap|Line Numbers
  1. My DOG name 
  2. *** CAT is in the kitchen
  3. Name DOG
  4. *** CAT is in the kitchen
  5. My DOG name
  6. My DOG name
  7. My DOG name
  8. My DOG name
  9.  
  10. print $animals | grep 'DOG' | nawk '{print $2}'
Desired Output:
Expand|Select|Wrap|Line Numbers
  1. DOG
  2. Not Found
  3. DOG
  4. Not found
  5. DOG
  6. DOG
  7. DOG
  8. 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
Mar 23 '08 #2

eWish
Expert 100+
P: 971
He posted this question on at least 3 other forums. Likely that there was not an attempt made.

--Kevin
Mar 23 '08 #3

P: 7
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:

Expand|Select|Wrap|Line Numbers
  1. #! c:\perl\bin\perl
  2.  
  3. use Net::Telnet;
  4.  
  5. @look=$net->cmd("cat $file | grep -i 'DOG' | awk 'NR==1' | nawk '{print \$2}');
  6. for $find (@look) {
  7. $find =~ s/(^DOG)/\uNot Found!/g;
  8. print "$find\n"; 
  9. }
  10.  
I intentionally replaced my standard input file due work related terminologies. =)

Tnx so much indeed for your ideas and tips.

Br,
Mar 24 '08 #4

P: 3
Expand|Select|Wrap|Line Numbers
  1. open (IN, "lines.txt");
  2. @words = <IN>;
  3. foreach my $m (@words)
  4. {
  5. if ($m=~/DOG/)
  6. {
  7.     $m=~s/.*DOG.*/DOG/g;
  8. }
  9. else
  10. {
  11.     $m=~s/^.*$/NOT FOUND/g;
  12. }
  13.  
  14. print $m;
  15.  
  16. }
Mar 27 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.