Howto: Search between 2 files

J

josetg

Hi,

Am a absolute newbie.

I have a list of keywords in one file, and the text to search for in 2nd file.
I need to find lines in the 2nd file which match keywords in the 1st file.

The output should be sorted by line # in 2nd file.

Many thanks
Jose
 
S

Sherm Pendley

josetg said:
Am a absolute newbie.

Welcome aboard. Here are some links to help you get started:

<http://learn.perl.org>
<http://perldoc.com/perl5.8.0/pod/perlfaq.html>

Lots of documentation is installed on your machine along with Perl itself.
To read it, use the 'perldoc' command-line tool. It can show you a specific
documentation page, module doc, function description, or find a keyword in
the list of FAQs.

Some examples:

Read the "perl" page: "perldoc perl"
Read about the "strict" module: "perldoc strict"
Read about the "grep" function: "perldoc -f grep"
Look for the keyword "strict": "perldoc -q strict"
I have a list of keywords in one file, and the text to search for in 2nd
file. I need to find lines in the 2nd file which match keywords in the 1st
file.

The output should be sorted by line # in 2nd file.

What have you tried so far?

sherm--
 
R

Richard Gration

Hi,
Am a absolute newbie.
I have a list of keywords in one file, and the text to search for in 2nd
file. I need to find lines in the 2nd file which match keywords in the
1st file. The output should be sorted by line # in 2nd file. Many
thanks
Jose

Not a perl solution but ... are you on unix? If so

grep -f keyword_file search_file

should do what you want. Put your keywords one per line in the
keyword file. If you want complete word matches only (eg "one" in
the keyword file matches "one" in the search file but *not* "done") then
surround the keywords with \< and \> (ie for case above put "\<one\>" in
the keyword file).

HTH
Rich
 
J

Jim Cochrane

Hi,

Am a absolute newbie.

I have a list of keywords in one file, and the text to search for in 2nd file.
I need to find lines in the 2nd file which match keywords in the 1st file.

The output should be sorted by line # in 2nd file.

It often helps to break a problem into smaller subproblems. In your case,
the subproblems are pretty obvious - here's one way to split them up:

my @keywords = keywords_from_file('keywordfile');
my @target_lines = lines_from_file('datafile');
for my $word (@keywords) {
# Search for $word in @target_lines, save matches, with line #
# (This is the hardest part. You might want to get the steps before
# the loop working before you tackle this. Ask here for help if you
# need it, of course.)
}
# Sort the results from the above loop and output the result.

sub keywords_from_file {
# implementation to be defined - ask here if you have trouble with it.
}

sub lines_from_file {
# implementation to be defined ...
}
 
B

Brad Baxter

Am a absolute newbie.

I have a list of keywords in one file, and the text to search for in 2nd file.
I need to find lines in the 2nd file which match keywords in the 1st file.

The output should be sorted by line # in 2nd file.

As an absolute newbie, this somewhat facetious answer might not help you.

perl -F'\W' -ane'if($x){(@a=grep$_{$_},@F)&&print"$.: @a:
$_"}else{map++$_,@_{@F}};eof&&($x++,$.=0)' file1 file2

This works for some definitions of 'keywords' and 'match'.

Regards,

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top