Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case Insensitive Search using GREP 2

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

I created a very basic Perl script that can grep item inside a file and display the result on the screen.

Code:
#!C:\Perl\bin\perl.exe

open (FILE, '<Fruits.txt')or die "$!\n";

@array = <FILE>;
@result = grep (/apple/, @array);
print @result;

close (FILE);


[blue]Fruits.txt[/blue] contains these lines:

apple
banana
cherry

If I use "grep -i" in Unix it works. How can I use that option in my script? Sorry this question is very basic.

Regards
viadisky
 
Code:
@result = grep (/apple/i, @array);

It's wasteful to save your entire file in an array that you're only using to grep. You can do it directly from the filehandle:
Code:
@result = grep (/apple/, <FILE>);
 
Hi ishnid,

Thank you very much for your suggestion. It worked!!!
I added something that would make the script dump the output to a text file instead.
[blue]Is there an easy way of dumping what is inside an array to a text file?[/blue] I used "foreach" loop in this case.

Code:
#!C:\Perl\bin\perl.exe

open (FILE, '<Names.txt')or die "$!\n";
open (OUT, '>>Output.txt');

@result = grep (/Apple/i, <FILE>);

foreach $fruit (@result)
{
    print OUT $fruit;
}

close (FILE);
close (OUT);

Regards
viadisky
 
This will have the same effect (assuming you haven't changed any of Perl's special variables):
Code:
print OUT @fruit;
 
Thanks again!

I used this instead ..

Code:
print OUT @result;

Regards
viadisky
 
Why create the temporary data structure at all? Just do line by line processing:

Code:
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]FILE, [red]'[/red][purple]names.txt[/purple][red]'[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black][red]([/red]OUT, [red]'[/red][purple]>>output.txt[/purple][red]'[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple][blue]$![/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[olive][b]for[/b][/olive] [red]([/red]<FILE>[red])[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUT [olive][b]if[/b][/olive] [red]/[/red][purple]Apple[/purple][red]/[/red][red]i[/red][red];[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]FILE[red])[/red][red];[/red]
[black][b]close[/b][/black][red]([/red]OUT[red])[/red][red];[/red]

This prevents anything from being saved needless to memory. Not to mention it's easier to read in my opinion, not that it's difficult either way. :)

- M
 
If you're doing that, you can avoid the "for" loop altogether:
Code:
print OUT grep (/Apple/i, <FILE>);
 
ishnid said:
If you're doing that, you can avoid the "for" loop altogether.

Of course you could avoid the for loop, but I tend to treat that as the barrier beyond which efficiency in typing is sacrificing maintainability.

As a rule I always go with line-by-line processing unless there is a specific reason not to. It avoids the need to think about whether the file is going to be too large to process in memory. And blocking the for loop gives you the easy option to add additional processing steps without having to reformat your code.

If I do in memory processing of a file, I use the File::Slurp module. This doesn't really save that much code. However, it's my way of acknowledging that I'm specifically not doing line-by-line processing. And it the nice side effect of making the code a little easier to read.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Slurp[/green][red];[/red]

[maroon]append_file[/maroon][red]([/red][red]'[/red][purple]output.txt[/purple][red]'[/red], [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][red]/[/red][purple]Apple[/purple][red]/[/red][red]i[/red][red]}[/red] [maroon]read_file[/maroon][red]([/red][red]'[/red][purple]names.txt[/purple][red]'[/red][red])[/red][red])[/red][red];[/red]
[tt]------------------------------------------------------------
Other Modules used :
[ul]
[li]File::Slurp[/li]
[/ul]
[/tt]

PS
My original code was missing a bracket, and I would have normally done a "while" there instead of a "for".

- Miller
 
Hi MillerH/ishnid,

Thanks for all the suggestions you provided. I was able to create the script I needed and at the same time I applied the same concepts to other scripts I'm doing.

Knowledge of both Perl and REGEX is very useful :)

Regards
viadisky

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top