[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Text file word search

Posted on 2004-10-02
2
Medium Priority
?
227 Views
Last Modified: 2012-06-27
I have a text file called cool.txt:

lovelovelove.com  Cool1
painpainpain.com  Cool2
expertexpert.com  Cool2

I want this: perl script.pl cool.txt cool1 cool1.txt

the script search the world cool1 in the file cool.txt and creat a file called cool1.txt and past the site(only the site).

cool1.txt:
lovelovelove.com


tnx.
0
Comment
Question by:cels9
2 Comments
 
LVL 10

Accepted Solution

by:
rj2 earned 2000 total points
ID: 12208612
if(scalar(@ARGV) < 2) {
      print "Usage: perl script.pl searchfile searchtext outfile\n";
      exit;
}
open(INFILE,$ARGV[0]) || die("Can not open infile $ARGV[0] because $!");
open(OUTFILE,">$ARGV[2]") || die("Can not open outfile $ARGV[2] because $!");
$text=$ARGV[1];
while(<INFILE>) {
      #assumes case insenstive search, remove trailing i if not
      #also assumes that matching only part of the final string is ok. Add $ before /i if not
      if(m/(^[^\s]+)\s+$text/i) {                                                                                                  
            print OUTFILE $1,"\n";
      }
}
close(INFILE);
close(OUTFILE);
0
 
LVL 85

Expert Comment

by:ozo
ID: 12210224
/[^\s]/ can also be written /\S/
You might also want to be aware of possible metacharacters in $text
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question