473,461 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Replacing a search/not found pattern with a word

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
4 1370
numberwhun
3,509 Expert Mod 2GB
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
971 Expert 512MB
He posted this question on at least 3 other forums. Likely that there was not an attempt made.

--Kevin
Mar 23 '08 #3
pinpe
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
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

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

Similar topics

8
by: Sharif T. Karim | last post by:
I am trying to do the following with my search script that looks for records in a mysql table. The following is an example of what I am trying to do. Text being searched: -- The brown fox...
0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
10
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no...
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
3
by: =?Utf-8?B?UGVycmlud29sZg==?= | last post by:
Not sure where to post this... Found some interesting behavior in Windows Search (Start =Search =All files and folders =search for "A word or phrase in the file:"). This applies to XP and maybe...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.