Solved

Text file word search

Posted on 2004-10-02
2
219 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 500 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 84

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
HTTP Error 502.2 - Bad Gateway 3 222
cpan issue 1 65
Need help with bash and/or perl commands on OS X Terminal 9 110
Question about @INC variable in perl 1 65
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

808 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