CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Issue with search and capture string from a file

 



wraz
Novice

Dec 14, 2007, 2:50 PM

Post #1 of 21 (2748 views)
Issue with search and capture string from a file Can't Post

This is the contents of my input file. I want to browse through this file and look for "im_im_rdy_samp". If this keyword is found, i want to then pick the string on the next line (in this case FRST_IM_FILE_IO_AMP) and store it in a variable.

dogy #1050 fi_dy_watch ("im_im_rdy_samp",
`FRST_IM_FILE_IO_AMP.Q,
`FRST_IM_FILE_IO_AMP.D,
`FRST_IM_FILE_IO_AMP.`CP,
`FRST_IM_FILE_IO_AMP.`flag);

//---------------Code

while(<f1>) {
chomp;
if($_ =~ /dogy/)
{
@temp = split(/\(\"/, $_);
@temp = split(/\"/, $_);
chop($temp[1]);
$sync_cap = $temp[1];
print "$sync_cap\n";
}
if($_ =~ $sync_cap)
{
@temp = split(/`/, $_);
chop($temp[0]);
print "$temp[0]\n";
}
}


With the raw code posted above, i am able to search for the string (im_im_rdy_samp) and the code displays the first line (fi_dy_watch).

I am not able to go to the next line and pick the string starting with this "`" character.

I am pretty new to Perl. Can some-body please help me with this?

Thanks,

wraz


NexisPerl
New User

Dec 14, 2007, 5:39 PM

Post #2 of 21 (2744 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

@temp = split(/\(\"/, $_);

REmove this line from your code.
------
Nexis
http://startperl.blogspot.com/2007/12/executing-commands-on-remote-machine.html


KevinR
Veteran


Dec 14, 2007, 5:40 PM

Post #3 of 21 (2744 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post


Code
while(<f1>) { 
if(/dogy/) {
$next_line = <f1>;
#do something with $next_line;
}
}


but why are you searching for "dogy" when you say you want to find "im_im_rdy_samp"?
-------------------------------------------------


wraz
Novice

Dec 15, 2007, 12:20 AM

Post #4 of 21 (2734 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

hi,

You are right Kevin. I should not be matching with dogy, instead i can match it with im_im_rdy_samp. But my main issue was that i wanted to get to the next line after matching the string and capture the first element on that line which i hope to get now with your solution. But i have a couple more issues. I have uploaded one document giving the exact nature of my task.

I would appreciate if you can help me on this.

Thanks..
Attachments: Task.docx (13.4 KB)


KevinR
Veteran


Dec 15, 2007, 9:01 AM

Post #5 of 21 (2729 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

I you have more questions, post them in this thread.
-------------------------------------------------


wraz
Novice

Dec 15, 2007, 10:39 AM

Post #6 of 21 (2727 views)
Re: [KevinR] Issue with search and capture string from a file [In reply to] Can't Post

Kevin. I have pasted the exact nature of my task below. I would appreciate if you can reply as soon as possible.

Thanks.



Task:

1. See the format of the log file pasted below.

//------------------------------------------------------------------------------------------------------------------------------------------

Violation

$setuphold<setup>( posedge CP &&& xD_check:356262080 PS, negedge D:356262040 PS);

Scope: top.desi.blk_logic.file_mclk_sync_a_reg_0_

Time: 356262080 PS

35626208: Note: saw notifier change on ogic_file mclk_sync_a_reg0, forcing to 0 for 1 clock cycle

//------------------------------------------------------------------------------------------------------------------------------------------

Task:

I want to process this log file in the example shown above.

Browse through the log file. Capture the design hierarchy in the Scope field. See if there is a Note with notifier message. If the Note is seen, then compare the design hierarchy in the scope field with the one in the notifier. If both match then move it to some log file (A1.log). If there is no Note after the violation then move it to another log file ( A2.log).

If you see the hierarchy in the Scope field and the Note field, they do not match. So additional processing is required. Browse through the Note field and capture the some part of the string (for ex mclk_sync_a_reg0). Now open another file(letís say hier.v) which has format as shown below. Search for (mclk_sync_a_reg2) which is seen in the watch field in the hier.v file. When this matches, go to the next line below the watch and pick the string ( for ex FILE_CLK_SYNC_REG_2_). Now finally to get the exact hierarchy, compare it with the field in define section of the hier file.

Final part is when the design hierarchy in the scope matches with the one in the Note part, move all the lines under the violation field to A1.log . Also move the Note line in A1.log.

//---------------------------------------

Hier.v file format:

//----------------------------------------

'define FILE_CLK_SYNC_REG_2_ 'top.desi.blk_logic.file_mclk_sync_a_reg_0_

watch #50 ablk_logic_reg2_watch ("logic_regfile mclk_sync_a_reg0 ",

`REGFILE_CLK_SYNC_REG_2_.Q);


KevinR
Veteran


Dec 15, 2007, 11:17 AM

Post #7 of 21 (2724 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

That is not a question. That is your task/project requirements which looks like a school/class assignment. I will only help you with code (if I can) that you post and specific questions you have about your code or general perl questions, like: how do I sort an array?
-------------------------------------------------


wraz
Novice

Dec 15, 2007, 4:36 PM

Post #8 of 21 (2720 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

Kevin, i face a very simple issue and i am not able to resolve it. Please help me.

while(<fi>) {
if($_ =~ /Scope:/)
{
@temp = split(/\./, $_);
}
if($_ =~ /Note:/)
{
@temp = split(/\W+/, $_);
}
}
$line = $temp[7];


With the above piece of code, i am able to get a particluar part of the string in $line variable. Next part is to open another file and search for the string captured in $line variable. But iam not able to get to the next line

input file ex: watch abcd_in_trf ("sync sync_a"),

`TRF_SYNC_DOG.Q,

`TRF_SYNC_DOG.IN);

while(<f1>) {
chomp;
if($_ =~ $line) {
$next_line = <f1>;
print "$next_line[0]\n";
}

With the above code, iam intertested to capture the string TRF_SYNC_DOG and display it.


wraz
Novice

Dec 15, 2007, 5:10 PM

Post #9 of 21 (2718 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

Kevin, i am able to capture the required string with the $next_line variable as suggested by you. Thanks for your help. Please ignore my last message in this thread.

I have one more question going ahead. I want to search for this string (SYNC_REG_IM_NIT) in the file. This string is seen in multiple places. I want to get the one which starts with define dogy. If there is a match, i want to move the rest of the string to a variable (in this case blk.sit.ccf.gnu.sync_h). How can i do it?

Input file:

`define dogy SYNC_REG_IM_NIT `blk.sit.ccf.gnu.sync_h

dogy watch ("syn in_im_reg",

SYNC_REG_IM_NIT.Q,

SYNC_REG_IM_NIT.D);


(This post was edited by wraz on Dec 15, 2007, 5:18 PM)


KevinR
Veteran


Dec 16, 2007, 9:56 AM

Post #10 of 21 (2711 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

You will most likely need to use a regexp to find the string and capture the part of the string you want to store in a variable.
-------------------------------------------------


wraz
Novice

Dec 17, 2007, 10:33 AM

Post #11 of 21 (2704 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

Input is in the form

def PARAM `ecc.mkin.cpp

I am trying to search for PARAM in a file and if it found, i would like to display (ecc.mkin.cpp). I tried using split based on (\W) and (\.) but not able to capture this string. Am i doing something fundamentally wrong? Please advise.

Code:while(<f1>) {
chomp;
if($_ =~ $match_string_1_2) {
@temp = split(/\./, $_);
print "$temp[0]\n";
print "$_\n";
}
}


KevinR
Veteran


Dec 17, 2007, 3:21 PM

Post #12 of 21 (2700 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

what is $match_string_1_2 ?

While I think this construct is possible:

if($_ =~ $match_string_1_2) {

usually a regexp has delimiters around the pattern:

if($_ =~ /$match_string_1_2/) {

because you are spliting the string on the dots @temp will equal these three values in this order:

def PARAM `ecc
mkin
cpp

if the tik (`) is always present you could split on that instead.
-------------------------------------------------


wraz
Novice

Dec 17, 2007, 3:38 PM

Post #13 of 21 (2697 views)
Re: [KevinR] Issue with search and capture string from a file [In reply to] Can't Post

Thanks Kevin. I got it. I was doing a silly mistake. I had a small code on top of the one i published which was also accessing the same file. I was again trying to open the same file in the next part of the code without closing it. This was the reason why no code was getting printed from the latter part.


wraz
Novice

Dec 17, 2007, 5:09 PM

Post #14 of 21 (2694 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

I would like to embed the pattern search in a subroutine and play with the result if the match is found. I tried with the foll code but it gives me error message (Split loop at perl.pl). Do i need to handle the split command in a particular fashion when using subroutines? Please guide me on this.

//Subroutine call

$result = &check_hierarchy;
if($status eq "Success")
{
print "Success\n";
}
else
{
print "Failure\n";
}


//Subroutine definition

sub check_hierarchy
{
while(<f2>) {
chomp;
if($_ =~ $match_string_1_2) {
@temp = split(/\W+/,$_);
}
}
$matched_string = $temp[3].".".$temp[4].".".$temp[5];

if($cmp_str_scope =~ $matched_string) {
print"$cmp_str_scope\n";
print"$matched_string\n";
return("Success");
}
close(f1);
}


KevinR
Veteran


Dec 17, 2007, 5:39 PM

Post #15 of 21 (2692 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

Post the error messages you are getting.
-------------------------------------------------


wraz
Novice

Dec 17, 2007, 7:48 PM

Post #16 of 21 (2689 views)
Re: [KevinR] Issue with search and capture string from a file [In reply to] Can't Post

Error message seen is as shown below. I get the error on the split command that i am using in the script.

(Split loop at perl.pl).

One more update. I am calling the subroutine after opening a file. It is embedded within the while <f1> loop.


KevinR
Veteran


Dec 17, 2007, 8:46 PM

Post #17 of 21 (2688 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

I have never seen that error before. A google search seems to indicate it is associated with RH 8 and the perl distribution that comes with it. See this thread on perlmonks for a discussion of this unusual error message:

http://www.perlmonks.org/?node_id=206001
-------------------------------------------------


wraz
Novice

Dec 18, 2007, 11:52 AM

Post #18 of 21 (2679 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

Thanks Kevin for the inputs. I tried defining the use bytes pragma and the error is not seen anymore. I have a query on printing multiple lines based on the result that i get from the subroutine call. If the subroutine return Success, then i want to print 7 lines. My issue is i want to start printing from 2 lines before i see the Scope string. Is there an easier method to handle this?

I have foll code:

while(<fi>) {
if($_ =~ /Scope:/)
{
@temp = split(/\./, $_);
$cmp_str_scope = $temp[3].".".$temp[4].".".$temp[5];
print "$cmp_str_scope\n";
}
if($_ =~ /Note:/)
{
@temp = split(/\W+/, $_);
$line = $temp[7];
print "$line\n";
$result = &check_hierarchy;
if($result eq "Matched")
{
print "Success\n";
}
else
{
print "Failure\n";
}
}
}


KevinR
Veteran


Dec 18, 2007, 3:18 PM

Post #19 of 21 (2674 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

You can use the Tie::File module (comes with perl) to read and write to a file in the manner you are wanting to do. You will need to change your code a bit to adapt it to using Tie::File. Or you could read the entire file into an array and loop through the array and jump around inside the array (back two lines for example). Then print the modified array back to a file. There might be other possibilities too.
-------------------------------------------------


wraz
Novice

Dec 20, 2007, 3:52 PM

Post #20 of 21 (2653 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

I have a query on $next_line parameter. In my code, i use $next_line to point my file handle to next line and capture the reuired portion of the string. The next part of the code also has to search for some string but it seems now the file handle contains only one line of code from the file. My question is how do i point my file handle back to the position before i jumped to the next line.

Thanks.


KevinR
Veteran


Dec 20, 2007, 7:22 PM

Post #21 of 21 (2651 views)
Re: [wraz] Issue with search and capture string from a file [In reply to] Can't Post

I aready told you how, use the Tie::File module.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives