![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sed: find match and delete the line above | cstovall | Shell Programming and Scripting | 3 | 07-02-2008 10:31 PM |
| Perl: Match a line with multiple search patterns | Juha | Shell Programming and Scripting | 10 | 04-09-2008 01:43 AM |
| Multiple line match using sed | SiftinDotCom | Shell Programming and Scripting | 15 | 03-28-2008 02:12 PM |
| read and match multiple lines in perl | zx1106 | Shell Programming and Scripting | 5 | 03-14-2008 10:21 PM |
| sed - Replace Line which contains the Pattern match with a new line | kousikan | Shell Programming and Scripting | 2 | 03-24-2007 07:24 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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,,,,,,,, Code:
*Init End *Main Start repeat(5); process all lines as usual here. ............ *Main End,,,,,,,,, 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; regards, user_prady Last edited by user_prady; 03-28-2008 at 12:51 AM.. |
|
||||
|
Is it not possible!!
Quote:
Thanks Last edited by user_prady; 03-30-2008 at 10:07 AM.. |
|
||||
|
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 = $_'
Last edited by era; 03-30-2008 at 06:22 AM.. Reason: Add a Perl snippet for remembering previous line |
|
||||
|
Quote:
|
|
||||
|
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.
|
|
||||
|
how to do it in perl
Quote:
I am trying to do the follwoing in perl Code:
nawk ' {
if($0 ~ /^\*Main Start/){
while( $0 !~ /^\*Main End/){
print $0;
getline;
}
}
} ' my.txt
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 01:48 AM.. |
![]() |
| Bookmarks |
| Tags |
| perl, perl regex, perl shift, regex, shift, shift perl, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|