Search the web
Sign In
New User? Sign Up
Perl_Official · Perl . CGI . Shell script
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
I found an answer to BackReference, your input?   Message List  
Reply | Forward Message #2046 of 2062 |
Re: How to search particular words in a file

Avinash,

Here's one solution.

Darren

====

#!/bin/perl/bin

###
### title: pgrep
### author: darren miller
###
### perl alternative to UNIX grep command,
### which searches for patterns in a file.
### exhanced version of the program by the same
### name in the camel book from O'Reilly.
###
### usage: pgrep -i -e "html" -e "form" *.html *.htm
###
### -i = ignore case
### -e = identifies a regular expression to search for
###

my @expressions; # array of regular expressions to search for
my @fileglobs; # array of filename patterns to search in
my $ignorecase="false"; # ignore case?

while ($param = shift(@ARGV)) {
if ($param eq "-e") {
push @expressions, shift(@ARGV);
} elsif ($param eq "-i") {
$ignorecase = "true";
} else {
push @fileglobs, $param;
}
}

foreach $fglob (shift(@fileglobs)) { # loop
through file name patterns
foreach $f (glob($fglob)) { # loop
through files that match the current pattern
if (open FH, "<$f") { # open
the file
my $line=0; #
initialize a line counter
while (<FH>) { # loop
through the current file
$line++; #
increment counter
foreach $expression (@expressions) { # loop
through expressions
if ((m/$expression/) or ($ignorecase eq "true" and m/
$expression/i)) { # check for a match
printf "%s (%.6d): %s", $f, $line, $_; # print
the file name, line number, and line
}
}
}
}
}
}


--- In Perl_Official@yahoogroups.com, avinash k <avik1612@...> wrote:
>
> hi,
>
> I am trying to find particular words in text files(i.e. *.txt
files)
>
> Should i use regular expressions in doing?
>
> Thanks in advance
> Avinash





Sat Sep 8, 2007 5:07 pm

millerdw2003
Offline Offline
Send Email Send Email

Forward
Message #2046 of 2062 |
Expand Messages Author Sort by Date

#!/usr/bin/perl use strict ; my @Name; my $Name; my $X = 1; my $Y = 2; my $Data=":2-Lou-14:4-Bobby:3-Tine13:1-jack 23:"; my $Info = ($Data =~ /:$Y-(.*?):/); ...
Lou Hernsen
luinrandir
Offline Send Email
Aug 31, 2007
5:00 pm

hi, I am trying to find particular words in text files(i.e. *.txt files) Should i use regular expressions in doing? Thanks in advance Avinash ... Get the...
avinash k
avik1612
Offline Send Email
Sep 7, 2007
12:03 am

Hello Avinash, yes u can easily find the word and its line through regx. here i have just given a simple example to your reference. im also preparing the perl...
K. Senthil Kumar
senmal2003
Offline Send Email
Sep 9, 2007
2:53 am

Avinash, Here's one solution. Darren ==== #!/bin/perl/bin ### ### title: pgrep ### author: darren miller ### ### perl alternative to UNIX grep command, ###...
millerdw2003
Offline Send Email
Sep 9, 2007
3:03 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help