if match found go to a particular line in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if match found go to a particular line in perl
# 1  
Old 03-27-2008
Data if match found go to a particular line in perl

Hello Experts,

I am newbie to perl, just curious to know how to do the following in perl.
suppose I ve a txt file like below. when it founds "*Main Start"
Then go to "*Main End,,,,,,,," patteren and just collect the number from the previous line of "*Main End,,,,,,," pattern . In my case it is 5 . Then start excuting normally from the "*Main Start" position

Following is the picture .. I dont want to create any temp file also..
Input file:
Code:
*Init End
*Main Start
*Comment Reset Timers
000000,0000,0,0,0,0,0,1,0
000000,0000,0,0,0,0,1,1,0 
*Comment Control Frame at 1.04596 ms
000000,0400,0,0,0,0,1,0,1
2418A4,0000,0,1,3,0,0,0,0 
049C00,0000,0,0,2,0,0,0,0
*Comment Control Frame at 1.04673 ms
*Comment Control Frame at
000002,0000,0,0,0,0,1,0,1
241002,0000,0,1,3,0,0,0,0
000100,0000,0,0,2,0,0,0,0
*Comment Control Frame at
000004,0000,0,0,0,0,1,0,1
241002,0000,0,1,3,0,0,0,0
000000,0000,0,0,2,0,0,0,0
*Comment Frame 13 at ** us,,,,,,,,
000005,7E3D,0,0,0,0,1,0,1
*Main End,,,,,,,,

output:
Code:
*Init End
*Main Start
 repeat(5);
process all lines as usual here.

............
*Main End,,,,,,,,,

I ve to print all lines as it is when it found a line starting with * . Thats why I am doing like below
Code:
 if(m/^\*/){
              print "//", $_;
              next;
    }

Code:
Basic Algorithm of my programme need :
1. If "*Main Start" pattern found go to line where Pattern matches "*Main end".
2. Get the previous line. and get the value of the first field.
3. Return to "*Main start" position again. & Print repeat(that number) : in my case it is 5 :
4. stop/next;

Any help most appriciated..

regards,
user_prady

Last edited by user_prady; 03-28-2008 at 01:51 AM..
# 2  
Old 03-30-2008
Is it not possible!!

Quote:
Originally Posted by user_prady
Hello Experts,

I am newbie to perl, just curious to know how to do the following in perl.
suppose I ve a txt file like below. when it founds "*Main Start"
Then go to "*Main End,,,,,,,," patteren and just collect the number from the previous line of "*Main End,,,,,,," pattern . In my case it is 5 . Then start excuting normally from the "*Main Start" position

Following is the picture .. I dont want to create any temp file also..
Input file:
Code:
*Init End
*Main Start
*Comment Reset Timers
000000,0000,0,0,0,0,0,1,0
000000,0000,0,0,0,0,1,1,0 
*Comment Control Frame at 1.04596 ms
000000,0400,0,0,0,0,1,0,1
2418A4,0000,0,1,3,0,0,0,0 
049C00,0000,0,0,2,0,0,0,0
*Comment Control Frame at 1.04673 ms
*Comment Control Frame at
000002,0000,0,0,0,0,1,0,1
241002,0000,0,1,3,0,0,0,0
000100,0000,0,0,2,0,0,0,0
*Comment Control Frame at
000004,0000,0,0,0,0,1,0,1
241002,0000,0,1,3,0,0,0,0
000000,0000,0,0,2,0,0,0,0
*Comment Frame 13 at ** us,,,,,,,,
000005,7E3D,0,0,0,0,1,0,1
*Main End,,,,,,,,

output:
Code:
*Init End
*Main Start
 repeat(5);
process all lines as usual here.

............
*Main End,,,,,,,,,

I ve to print all lines as it is when it found a line starting with * . Thats why I am doing like below
Code:
 if(m/^\*/){
              print "//", $_;
              next;
    }

Code:
Basic Algorithm of my programme need :
1. If "*Main Start" pattern found go to line where Pattern matches "*Main end".
2. Get the previous line. and get the value of the first field.
3. Return to "*Main start" position again. & Print repeat(that number) : in my case it is 5 :
4. stop/next;

Any help most appriciated..

regards,
user_prady
Hi friends is there anyway to do it. Pls help ..

Thanks

Last edited by user_prady; 03-30-2008 at 11:07 AM..
# 3  
Old 03-30-2008
Don't depend on me, I need to be going back to my day job soon.

There's a number of ways to do this, obviously. The old-fashioned variant would be to remember the previous line and print that when you see the terminator. The really brute Perl approach would be to slurp the whole file and substitute everything with an empty string except the line before the terminator. There's one in the Perl FAQ about that; perlfaq6 and scroll around for related questions. (The question about C comments further down the page has some hints, too.)

But the "previous line" solution is absolutely the simplest in this case, if you have no further requirements.

Code:
perl -ne 'BEGIN { $matching = 0; }
  $matching = 1 if (m/^\*Main Start/);
  next unless $matching;
  print $prev if (defined $prev && m/^\*Main End/);
  $prev = $_'

Not sure about the flow control you tried to describe. Are you supposed to remember everything from main start until main end and print that after the output from the last line before main end? (I guess this already qualifies as a pseudo-code implementation of what it takes. But then maybe I would consider a regex substitution over the whole file, or each *Main Start section, after all.)

Last edited by era; 03-30-2008 at 07:22 AM.. Reason: Add a Perl snippet for remembering previous line
# 4  
Old 03-30-2008
Quote:
Originally Posted by era
Don't depend on me, I need to be going back to my day job soon.

There's a number of ways to do this, obviously. The old-fashioned variant would be to remember the previous line and print that when you see the terminator. The really brute Perl approach would be to slurp the whole file and substitute everything with an empty string except the line before the terminator. There's one in the Perl FAQ about that; perlfaq6 and scroll around for related questions. (The question about C comments further down the page has some hints, too.)

But the "previous line" solution is absolutely the simplest in this case, if you have no further requirements.

Code:
perl -ne 'BEGIN { $matching = 0; }
  $matching = 1 if (m/^\*Main Start/);
  next unless $matching;
  print $prev if (defined $prev && m/^\*Main End/);
  $prev = $_'

Not sure about the flow control you tried to describe. Are you supposed to remember everything from main start until main end and print that after the output from the last line before main end? (I guess this already qualifies as a pseudo-code implementation of what it takes. But then maybe I would consider a regex substitution over the whole file, or each *Main Start section, after all.)
Thanks a Lot Era for your kindness..I ll try your mentioned way..
# 5  
Old 03-30-2008
I don't think I have captured all your requirements correctly but at least it's a start. If I am allowed to guess, what would be easiest is collect the output in a variable and print it after you have seen *Main End and printed out the line which immediately preceded it.
# 6  
Old 03-31-2008
how to do it in perl

Quote:
Originally Posted by era
I don't think I have captured all your requirements correctly but at least it's a start. If I am allowed to guess, what would be easiest is collect the output in a variable and print it after you have seen *Main End and printed out the line which immediately preceded it.
I tried almost all way using next , redo and last command in perl , But I can't get these syntax correctly in perl. I know its easy ,but still I cant find the efficient way using while and next statement in perl.

I am trying to do the follwoing in perl

Code:
nawk ' {
        if($0 ~ /^\*Main Start/){
	     while( $0 !~ /^\*Main End/){
	     print $0;
             getline;
        }
     }
 } ' my.txt

One more code I want to do it in perl pls give me some basic idea how to do it without creating any temporary files ..

Code:
TMP=/tmp/my_tmp$$
nawk '
/\*Main End/{
split(x,arr,",")
print "Loop  " arr[1]
};
{x=$0
}
' my.txt > $TMP 

nawk '{
if($0 ~ /^Loop/){
    loop = $2 
    next;
  }
if($0 ~ /^\*Main Start/){
     printf "\nrepeat ("
     print  loop

} ' $TMP $my.txt


Thanks & Regards,
user_prady

Last edited by user_prady; 03-31-2008 at 02:48 AM..
# 7  
Old 03-31-2008
Quote:
I am newbie to perl, just curious to know how to do the following in perl.
suppose I ve a txt file like below. when it founds "*Main Start"
Then go to "*Main End,,,,,,,," patteren and just collect the number from the previous line of "*Main End,,,,,,," pattern . In my case it is 5 . Then start excuting normally from the "*Main Start" position
You could use the Tie::File module for this which avoids the brute approach "era" mentioned. With Tie::File you treat a file like a perl array, so if you find a pattern in a certain line you just use simple array indexing to go back one line in the file. See the Tie::File (a perl core module) documentation for usage details.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

2. Shell Programming and Scripting

Add comment on last line if found match

Hi All, totally new on it , normally use it for just 1 line. i'm looking for help. i'm have 2 file. file 1 : -------------------------------------------------- c12 c1 c3 -------------------------------------------------- file 2: other content ... (10 Replies)
Discussion started by: kttan
10 Replies

3. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

4. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

5. Shell Programming and Scripting

How to print the entire line if the mentioned match is found?

Hello Everyone, I have a file with 5 fields in each line just like mentioned below. Also the 4th field is time elapsed(hh:mm:ss) since the process is running xyz abc status 23:00:00 idle abc def status 24:00:00 idle def gji status 27:00:02 idle fgh gty status 00:00:00 idle Here I... (8 Replies)
Discussion started by: rahul2662
8 Replies

6. Shell Programming and Scripting

Displaying text till pattern match found in a line

Hi All, From the below line if we want to display all the text till found pattern dot/. I was trying with the below code but couldn't able to print text before the pattern. it display texts which is found after pattern. awk '/assed/{print;getline;print}' file_name | sed 's/^*. *//' input... (4 Replies)
Discussion started by: Optimus81
4 Replies

7. UNIX for Dummies Questions & Answers

Display n lines after match found and other line

I have a file like this DoctorName Address1 Address2 DOB InsuredName Address1 Address2 DOB PatientName Address1 Address2 DOB ClaimNo1 DoctorName Address1 Address2 DOB InsuredName (2 Replies)
Discussion started by: nsuresh316
2 Replies

8. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

9. Shell Programming and Scripting

help with script to send email and if subject line match is found

Help with script that will check log, then find a match is found, add that as the subject line. 1. The script will always run as a deamon.. and scan the event.log file 2. when a new 101 line is added to the event.log file, have the script check position 5,6 and 7 which is the job name, which... (2 Replies)
Discussion started by: axdelg
2 Replies

10. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies
Login or Register to Ask a Question