how to get the position of match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get the position of match
# 1  
Old 09-15-2012
how to get the position of match

hi,
i want to identify the position of match in my substring here is the code
Code:
open (my $fhConditions, "<1.txt");
my $l = 100;
open(my $read, "<2.txt");
my @e = <$read>;
my $d = join('', @e );
$d =~ s/\s+//g;
while (my $line = <$fhConditions>) {
chomp $line;
my @info = split("\t" , $line);
my $a= $info[3]-$l;
my $match = substr($d,$a,$l);
print "$line\n" if $match =~m/ASHGFDF/;
}

1.txt
Code:
aa qq ee 22
jjh cc qa 200
iiu qq qq 150

——————
2.txt
Code:
AAAAAAAAAAMKNNNJJJBHGVFTCDRSEXXZASEDFRFGTHYUJIKOLLLPPLLOKIKKKKKKKKKKKKKKKKKKKASHGFDFKKBBVGFCDRRTRRCXXSEDRRFDDXSEEWAZZSA..................

Through the above program i was able to search in a substring of 100 characters whether my pattern is there but i want to know where in that 100 characters my pattern is matched. hope i am clear.

Last edited by Scrutinizer; 09-15-2012 at 05:01 AM.. Reason: code tags
# 2  
Old 09-15-2012
Quote:
Originally Posted by anurupa777
hi,
i want to identify the position of match in my substring here is the code
Code:
open (my $fhConditions, "<1.txt");
my $l = 100;
open(my $read, "<2.txt");
my @e = <$read>;
my $d = join('', @e );
$d =~ s/\s+//g;
while (my $line = <$fhConditions>) {
chomp $line;
my @info = split("\t" , $line);
my $a= $info[3]-$l;
my $match = substr($d,$a,$l);
print "$line\n" if $match =~m/ASHGFDF/;
}

1.txt
aa qq ee 22
——————
2.txt
AAAAAAAAAAMKNNNJJJBHGVFTCDRSEXXZASEDFRFGTHYUJIKOLLLPPLLOKIKKKKKKKKKKKKKKKKKKKASHGFDFKKBBVGFCDRRTRRCXXSEDRRFDDXSEEWAZZSA.
Through the above program i was able to search in a substring of 100 characters whether my pattern is there but i want to know where in that 100 characters my pattern is matched. hope i am clear.
I am not clear what exact you want...

Do you want the position of your search pattern??
By using below you can find end position of your search pattern and by subtracting with the length of the search pattern you get start and end position of the string. But this gives you only last string matched. If you have two matched screen then it will give position of last pattern..

Code:
$ Stext="Hello World"

$ expr "$Stext" : '.*World'
11   #prints position of d from World

#For start position 
var="World"
expr 11 - ${#var}

# 3  
Old 09-15-2012
Quote:
Originally Posted by pamu
I am not clear what exact you want...
Hi
i have 2 files
the info[3] of 1.txt contains (i,e., the the 4rth tab) some position.
the 2.txt contains just the characters. what my program presently does is extract the info[3] (the position) from file 1 and check for the pattern in file 2 starting from that position+100 (substr). it has to find the pattern with in that region. the program that i wrote performs till here. now the problem is where in that substr of 100 characters it is being matched. just check the code now.
Code:
open (my $fhConditions, "<1.txt");
my $l = 100;
open(my $read, "<2.txt");
my @e = <$read>;
my $d = join('', @e );
$d =~ s/\s+//g;
while (my $line = <$fhConditions>) {
chomp $line;
my @info = split("\t" , $line);
my $match = substr($d,$info[3],$l);
print "$line\n" if $match =~m/ASHGFDF/;
}

# 4  
Old 09-15-2012
Quote:
Originally Posted by anurupa777
now the problem is where in that substr of 100 characters it is being matched.
You want to calculate position of the matched pattern. I think I've already provided the way you can calculate this in my previous post. Just implement this.Smilie
# 5  
Old 09-15-2012
Speaking in perl...
Code:
open (my $fhConditions, "<1.txt");
 my $l = 100; open(my $read, "<2.txt");
 my @e = <$read>; my $d = join('', @e );
 $d =~ s/\s+//g;
 while (my $line = <$fhConditions>) {
 chomp $line;
 my @info = split("\t" , $line);
 my $a= $info[3]-$l; 
my $match = substr($d,$a,$l); 
print "$line\n" if $match =~m/ASHGFDF/;
 my $position=index($match,"ASHGFDF");
print $position+1; #prints the position where string is found
}

oops...sorry for messing up the format....
# 6  
Old 09-15-2012
index() is fine so long as you don't need to use a regular expression. Otherwise, the answer can be found @ perlvar - Variables related to regular expressions - perldoc.perl.org

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting value at a particular position without changing the position of other characters

Hi All, I wanted a sed/awk command to add a value/character on a particular position without disturbing the position of other characters. I have file a.txt OL 10031 Day Black Midi Good Value P01 P07 OL 10031 Day Black Short Good Value P01 P07 I want to get the output as... (2 Replies)
Discussion started by: rahulsk
2 Replies

2. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

3. Shell Programming and Scripting

Identify the First Column Position in Second Column and add the position value

Identify the First Column Position in Second Column and add the position value in 3rd column. Sample data: a|c b|d c|a d|b e|e f|g g|f |h |i Expected Output: a|c|1 b|d|2 c|a|3 d|b|4 (6 Replies)
Discussion started by: BrahmaNaiduA
6 Replies

4. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

6. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

Moving first position in a file to the last position

hi, I have a file which consists of some records: 2010_06_4010093_001_001|10|ABCDEFGH|9|4010093||0040400||31.12.2009|S|O|X||||20100602093851-31.12.2009|XXBBFC|EFG||||00001| 2010_06_4010162_001_001|11|ABCDEFGH|9|4010162||0040400||31.12.2009|S|O|X||||20100602093851-31.12.2009|XXBBFC|EFG||||00002|... (11 Replies)
Discussion started by: pparthiv
11 Replies

8. UNIX for Dummies Questions & Answers

find if a position is between a given start and end position

Hi, I am a newbie in unix programming so maybe this is a simple question. I would like to know how can I make a script that outputs only the values that are not between any given start and end positions Example file1: 2 30 40 80 82 100 file2: ID1 1 ID2 35 ID3 80 ID4 81 ID6... (9 Replies)
Discussion started by: fadista
9 Replies

9. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies

10. UNIX for Dummies Questions & Answers

Extracting lines that match string at certain position

I have a fixed length file in the following format <date><product_code><other data> The file size is huge and I have to extract only the lines that match a certain product code which is of 2 bytes length. I cannot use normal grep since that may give undesirable results. When I search for prod... (5 Replies)
Discussion started by: paruthiveeran
5 Replies
Login or Register to Ask a Question