[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Text file word search

Posted on 2004-10-02
2
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

650 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