Get all the values matched in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get all the values matched in perl
# 1  
Old 11-10-2009
Get all the values matched in perl

HI,

I have sentences like this:

Code:
@str=("An ribonucleic acid (RNA)-binding protein, started its expression in the daughter cells","Elucidation of the mechanism of retinal degeneration of RNA-binding protein","Rna binding protein is an important protein","In the retinal degenerative process following enucleation of rna binding protein");

If user queries with the term "rna binding protein" is should retrieve all the sentences.

The output should be like this.

Code:
An ribonucleic acid (RNA)-binding protein, started its expression in the daughter cells

Elucidation of the mechanism of retinal degeneration of RNA-binding protein

Rna binding protein is an important protein

In the retinal degenerative process following enucleation of rna binding protein

But the output i am getting is like this:

Code:
Elucidation of the mechanism of retinal degeneration of RNA-binding protein

Rna binding protein is an important protein

In the retinal degenerative process following enucleation of rna binding protein

I have used the following code.

Code:
$query="rna binding protein";
$regexp=$query;
$regexp=~ s/\s+/[- \\s]/gi;
foreach $str (@str)
{
if($str=~/\b$regexp\b/ig)
{
    print "<br> Match sentences --- > $str <br>";
}
}

The output i got was like this:

Code:
Elucidation of the mechanism of retinal degeneration of RNA-binding protein

Rna binding protein is an important protein

In the retinal degenerative process following enucleation of rna binding protein

But the output i want should be like this:

Code:
An ribonucleic acid (RNA)-binding protein, started its expression in the daughter cells

Elucidation of the mechanism of retinal degeneration of RNA-binding protein

Rna binding protein is an important protein

In the retinal degenerative process following enucleation of rna binding protein

How can i get all sentences for the specified term in perl?

Any suggestions??

Regards
Vanitha
# 2  
Old 11-10-2009
Code:
$query="rna binding protein";
$regexp=$query;
$regexp=~ s/\s+/[- \\s]/gi;
foreach $str (@str)
{
if($str=~/\b$regexp\b/ig)
{
    print "<br> Match sentences --- > $str <br>";
}
}

first: u don't rly know how much words will use user isn't ?
so it can be "rna binding protein" or "protein binding rna mechanism started"
if so u have to make regexp using () to define every word as $1 $2 $3 etc then use them in search through array

second: i didn't got this "$regexp=~ s/\s+/[- \\s]/gi;" at all.. what should that mean?
u replacing every 1+ spaces of any kind (space,tab,end of line etc) with [- \\s] ??
i believe u was trying to do s/[- \\s]/ /g; but did wrong

try to use this for your specific case (when there's 3 words for search):
Code:
$query="rna binding protein";
($regexp=$query)=~/(\w+?)\s+(\w+?)\s+(\w+?)/; # so it will get only letter/numbers and _ from search string

# or even like this:
($regexp=$query)=~/(\b\w+\b).+?(\b\w+\b).+?(\b\w+\b)/;

# or this
($regexp=$query)=~/(\S+)\s+(\S+)\s+(\S+)/;

foreach (@str)
{
# choose and use 1 of the examples
print "<br>Match sentences --> $_<br>\n" if /(?:$1|$2|$3)/gi; # in case if match any of those words
print "<br>Match sentences --> $_<br>\n" if /(?:$1.*?$2.*?$3)/i; # in case if match exactly as user said "rna binding protein" but ignoring any ) ( - etc and even if it will be "rnabindngprotein" - still match. but won't match if user searching rna-binding-protein
}

but still there can be many words or just 1 word so the code need more work on depending on your needs

Last edited by tip78; 11-10-2009 at 09:33 AM..
tip78
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print only matched pattern in perl

Hi, I have script like below: #!/usr/local/bin/perl use strict; use warnings; while (<DATA>) { ( my ($s_id) = /^\d+\|(\d+?)\|/ ) ; if ( $s_id == 1 ){ s/^(.*\|)*.*ABC\.pi=(+|+)*.*ABC\.id=(\d+|+).*$/$1$2|$3/s; print "$1$2|$3\n"; (2 Replies)
Discussion started by: sol_nov
2 Replies

2. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

3. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

4. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

5. Shell Programming and Scripting

Get header and its matched value in perl?

Hi, I have the file contents: Start,End,Req,Resp 12:39,12:40,4,5 The sting to be matched is: Req and Resp. parsefile("Req,Resp"); Here is the code. sub parsefile ($) { $header=shift; (2 Replies)
Discussion started by: vanitham
2 Replies

6. Shell Programming and Scripting

How to get the lines matched of a file in perl?

Hi, I want to match the time in the file and retrieve those contents of the file. I am taking only first two parameters of localtime(time) function minutes and seconds so partial match i am performing. For Example $start = "14:23"; $end = "14:30"; I am matching file contents... (3 Replies)
Discussion started by: vanitham
3 Replies

7. Shell Programming and Scripting

Remove matched values and their related groups

For each value in file1 it has to check in file2 and file3. If value matched it has to delete that value and related group value in file2 and file3. In this example it takes A , deletes A and take related group value 1 and deletes E-1,then checks in file3 and deletes K-1.After that it takes D... (7 Replies)
Discussion started by: kanagaraj
7 Replies

8. Shell Programming and Scripting

How to match all array contents and display all highest matched sentences in perl?

Hi, I have an array with 3 words in it and i have to match all the array contents and display the exact matched sentence i.e all 3 words should match with the sentence. Here are sentences. $arr1="Our data suggests that epithelial shape and growth control are unequally affected depending... (5 Replies)
Discussion started by: vanitham
5 Replies

9. Shell Programming and Scripting

print last matched pattern using perl

Hi, If there exist multiple pattern in a file, how can I find the last record matching the pattern through perl. The below script searches for the pattern everywhere in an input file. #! /usr/bin/perl -s -wnl BEGIN { $pattern or warn"Usage: $0 -pattern='RE' \n" and exit 255;... (5 Replies)
Discussion started by: er_ashu
5 Replies

10. Shell Programming and Scripting

HELP! PERL script to find matched pattern

Hi all, I just learnt Perl and I encountered a problem in my current project. For a verilog file, i am required to write a PERL script that could match pattern to output nitrolink and nitropack. I wont know what name to grep except the pattern below. the verilog file: nitrolink nitrolink... (1 Reply)
Discussion started by: kimhuat
1 Replies
Login or Register to Ask a Question