Solved

Text file word search

Posted on 2004-10-02
2
214 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
Comment Utility
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
Comment Utility
/[^\s]/ can also be written /\S/
You might also want to be aware of possible metacharacters in $text
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now