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

Need help regarding "search in file"

P: 4
Hi all,
I wrote a script to search a pattern in input file and if pattern does not found in input file ,print that pattern in to output file.

The input file is
--------------------------------------------------------
1999-1011ğAll the flowers of tomorrow are in the seeds of today.
2000-1209ğI saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts.
1999-1041ğAll the flowers of tomorrow are in the seeds of today.
2000-1367ğI saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts.
2000-6646ğI saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts.
2000-6463ğI saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts.
----------------------------------------------------------
The pattern i want to find is 1999-1041
The script I wrote is-
Expand|Select|Wrap|Line Numbers
  1. open (RD, "input.txt") or die;
  2. @Read = <RD>; close (RD);
  3.  
  4. open (WR2, ">>output.txt") or die;
  5. $count=0;
  6. $C="1999-1041";
  7. for ($read=0; $read <= $#Read; $read++)
  8. {
  9.     if($Read[$read] !~ /$C/)
  10.     {
  11.     $count++;
  12.     print WR2"$count=$C\n";
  13.     }
  14.  
  15. }
  16.  
---------------------------------------------
The output I got is

1=1999-1041
2=1999-1041
3=1999-1041
4=1999-1041
5=1999-1041

That means it search 6 lines in input file and found 5 lines which does not contain pattern,thats why it prints pattern 5 times to output file.
But actually the required output is none as pattern is present in input file.

Need guidelines regarding this ..

--Mahesh
Jan 29 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
You say you want to find the pattern but you tell the regexp to not find the pattern (!~):

Expand|Select|Wrap|Line Numbers
  1. if($Read[$read] !~ /$C/) 
should be:

Expand|Select|Wrap|Line Numbers
  1. if($Read[$read] =~ /$C/) 

anyways:

Expand|Select|Wrap|Line Numbers
  1. open (RD, "input.txt") or die;
  2. @Read = <RD>;
  3. close (RD);
  4.  
  5. open (WR2, ">>output.txt") or die;
  6. $C = "1999-1041";
  7. $count = 0;
  8. for (@Read ) {
  9.    $count++;
  10.    if(/$C/) {
  11.       print WR2 "$count=$C\n";
  12.    }
  13. }
Jan 29 '08 #2

P: 4
You say you want to find the pattern but you tell the regexp to not find the pattern (!~):

Expand|Select|Wrap|Line Numbers
  1. if($Read[$read] !~ /$C/) 
should be:

Expand|Select|Wrap|Line Numbers
  1. if($Read[$read] =~ /$C/) 

anyways:

Expand|Select|Wrap|Line Numbers
  1. open (RD, "input.txt") or die;
  2. @Read = <RD>;
  3. close (RD);
  4.  
  5. open (WR2, ">>output.txt") or die;
  6. $C = "1999-1041";
  7. $count = 0;
  8. for (@Read ) {
  9.    $count++;
  10.    if(/$C/) {
  11.       print WR2 "$count=$C\n";
  12.    }
  13. }
--------------------------------
I want to print a pattern if it is not found in the input file thats why i use
Expand|Select|Wrap|Line Numbers
  1. if($Read[$read] !~ /$C/) 
.do u know some solution?//
Jan 31 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
--------------------------------
I want to print a pattern if it is not found in the input file thats why i use
Expand|Select|Wrap|Line Numbers
  1. if($Read[$read] !~ /$C/) 
.do u know some solution?//
So, you want to print the line if the pattern is not found, correct? If that is the case, they you have a code issue. This line:

Expand|Select|Wrap|Line Numbers
  1. print WR2"$count=$C\n";
  2.  
is where you issue lies. You had set $C to be the pattern you are looking for. Then, you are printing whatever the count is followed by an equal sign, followed by the same value in $C. I think that what you are looking for is:

Expand|Select|Wrap|Line Numbers
  1. print WR2 ("$count = $Read[$read]\n");
  2.  
Regards,

Jeff
Jan 31 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
Ok, I tested what I just posted and it works as you wanted. Below is the code, followed by what was placed into the output.txt file. Just one note first though. When coding, you really need to implement the "use strict" and "use warnings" pragmas. They will help you avoid many of the cosmetic and simple coding errors that happen when entering your code and leave us with the real errors.

### The script ###
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. open (RD, "test.txt") or die;
  7. my @Read = <RD>; close (RD);
  8.  
  9. open (WR2, ">>output.txt") or die;
  10. my $count=0;
  11. my $C="1999-1041";
  12. for (my $read=0; $read <= $#Read; $read++)
  13. {
  14.         if($Read[$read] !~ /$C/)
  15.         {
  16.         $count++;
  17.         print WR2 ("$count = $Read[$read]\n");
  18.         }
  19.  
  20. }
  21.  
### The output ###
Expand|Select|Wrap|Line Numbers
  1. 1 = 1999-1011ğAll the flowers of tomorrow are in the seeds of today.
  2.  
  3. 2 = 2000-1209ğI saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts.
  4.  
  5. 3 = 2000-1367ğI saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts.
  6.  
  7. 4 = 2000-6646ğI saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts.
  8.  
  9. 5 = 2000-6463ğI saw this article posted on another site and wanted to share it with everyone at TSDN. Please read this article and post your thoughts.
  10.  
Regards,

Jeff
Jan 31 '08 #5

Post your reply

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