in reply to Re^4: Help with Search String
in thread Help with Search String

Your error is that you do print FILE "$_\n"; unconditionally so that every line of your input file is printed to your output file. There are other problems with your code.

The code becomes

my $outFile = q{/home/btobin/data3.txt}; open my $outFH, q{>>}, $outFile or die qq{open: $outFile: $!\n}; while ( <$dataFH> ) { print $outFH qq{Found name $_\n} if /($namesPatt)/; } close $outFH or die qq{close: $outFile: $!\n};

I hope this is helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^6: Help with Search String
by btobin0 (Acolyte) on Dec 08, 2007 at 06:48 UTC
    Thanks you for your help. This works great. Overwrite would be better. To change that I would need to change the line:
    open my $outFH, q{>>}, $outFile
    Correct?
Re^6: Help with Search String
by btobin0 (Acolyte) on Dec 08, 2007 at 09:29 UTC
    i have a question I have put this together and it works great in Solaris, but in Linux the search results show everything. Got any ideas?
    use strict; use warnings; my $namesFile = q{names.txt}; open my $namesFH, q{<}, $namesFile or die qq{open: $namesFile: $!\n}; my @names = <$namesFH>; close $namesFH or die qq{close: $namesFile: $!\n}; chomp @names; # Remove line terminators my $namesPatt = join q{|}, @names; my $dataFile = q{data2.txt}; open my $dataFH, q{<}, $dataFile or die qq{open: $dataFile: $!\n}; # while ( <$dataFH> ) # { # print qq{Found name $_\n} if /($namesPatt)/; my $outFile = q{/home/btobin/data3.txt}; open my $outFH, q{>}, $outFile or die qq{open: $outFile: $!\n}; while ( <$dataFH> ) { print $outFH qq{Found name $_\n} if /($namesPatt)/; } close $outFH or die qq{close: $outFile: $!\n}; # } close $dataFH or die qq{close: $dataFile: $!\n};
      To answer your previous post, yes, change q{>>} to q{>} as you have already discovered :-)

      Regarding Solaris versus Linux, I work mostly on Solaris but occasionally port my scripts to Linux. I have never had problems when doing that so I don't really know what might be going wrong. Check the obvious like making sure you have the same data files and script on both systems. After that it is probably a case of scattering extra print statements through you script to check that the data you think you are working with is in reality what you expected.

      Sorry I can't be of more help.

      Cheers,

      JohnGG