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

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-
open (RD, "input.txt") or die; @Read = <RD>; close (RD); open (WR2, ">>output.txt") or die; $count=0; $C="1999-1041"; for ($read=0; $read <= $#Read; $read++) { if($Read[$read] !~ /$C/) { $count++; print WR2"$count=$C\n"; } }

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

Replies are listed 'Best First'.
Re: Need help regarding "search in file"
by jwkrahn (Abbot) on Jan 29, 2008 at 09:26 UTC

    It sounds like you want something like this (untested):

    open my $RD, '<', 'input.txt' or die "Cannot open 'input.txt' $!"; open my $WR2, '>>', 'output.txt' or die "Cannot open 'output.txt' $!"; my $C = qr/1999-1041/; my $count = 0; while ( <$RD> ) { $count += /$C/; } print $WR2 "$C ", $count ? '' : 'not ', "found in file 'input.txt'.\n" +;
Re: Need help regarding "search in file"
by Punitha (Priest) on Jan 29, 2008 at 09:29 UTC

    Hi Mahesh,

    We have lot of ways to do this i can suggest you some ways,

    On customizing your code you can use like,

    use strict; use warnings; open (RD, "input.txt") or die; my @Read = <RD>; close (RD); open (WR2, ">>output.txt") or die; my $count=0; my $C="2001-1041"; my $found=0;#######Temporary variable to find the content in whole fil +e for (my $read=0; $read <= $#Read; $read++) { if($Read[$read] =~ /$C/) { $count++;$found++; } } print WR2"$count=$C\n" unless($found);

    In other way without reading the file in array, you can do like,

    use strict; use warnings; open (RD, "input.txt") or die; open (WR2, ">>output.txt") or die; my $count=0; my $C="2001-1041"; my $found=0;#######Temporary variable to find the content in whole fil +e while(<RD>){ if($_ =~ /$C/) { $count++;$found++; } } print WR2"$count=$C\n" unless($found);

    You can do like this also

    use strict; use warnings; open (RD, "input.txt") or die; my $input; do{ local undef $/; ######To undef the input record separator '$/' $input=<RD>; }; #####To read the whole file in single scalar variable open (WR2, ">>output.txt") or die; my $count=0; my $C="2001-1041"; while($input =~ /$C/g) { $count++; } print WR2 "$count=$C\n" unless($count);

    Punitha