Solved

Text file word search

Posted on 2004-10-02
2
216 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl to mysql 5 130
Need Helping Editing Perl Script for PowerChute Network Shutdown 3 77
use google analytics code in perl script 2 64
Replace  text in a file 2 97
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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

24 Experts available now in Live!

Get 1:1 Help Now