Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #8 (permalink)  
Old 03-31-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
got one way

Quote:
Originally Posted by user_prady View Post
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
At last I got one way to do it.


Code:
my @buffer;
$#buffer = 1;
open(IN, $file)|| die("Could not open file");
 
while(<IN>){
        push@buffer => $_;
        shift@buffer;
}
close(IN);
open(IN, $file)|| die("Could not open file");
while(<IN>){ 
       print $buffer[0] if/^\*Main Start/ ;
}
close(IN);

Regards,
prady
Sponsored Links
  #9 (permalink)  
Old 03-31-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by KevinADC View Post
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.
Thanks Kevin for your time and kind ..

But when I am trying to use the module Tie::File I am getting the follwing error.

Code:
Can't locate Tie/File.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .) at ./mdl.pl line 5.
BEGIN failed--compilation aborted at ./mdl.pl line 5.

This is first time I'll use a module so pls if I am doing something wrong with my code pls suggest me.


Code:
use Tie::File;
tie @array, 'Tie::File', $file or die ("Could not open file");
print $array[13] ;

Regards,
user_prady

Last edited by user_prady; 03-31-2008 at 04:14 AM..
  #10 (permalink)  
Old 03-31-2008
Registered User
 

Join Date: Jan 2008
Posts: 729
Tie::File has been a core module for a while, but I am not sure since when. The problem is that your server is using a very old version of perl 5.005, so either it was not included with the distribution of perl you have or it was removed for some reason.
  #11 (permalink)  
Old 03-31-2008
Registered User
 

Join Date: Jan 2008
Posts: 729
You could do something like this:


Code:
use strict;
use warnings;
my $flag = 0;
my $last_line;
while(<DATA>){
   if(/^\*Main Start/){
      $flag = 1;
   }
   elsif($flag && /^\*Main End/) {
         $numbers = (split(',',$last_line))[0];
         $last_line = '';
         $flag = 0; 
         print $numbers,"\n";
   }
   else {
      $last_line = $_;
      next;
   }
}	
__DATA__
*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,,,,,,,,

  #12 (permalink)  
Old 03-31-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by KevinADC View Post
Tie::File has been a core module for a while, but I am not sure since when. The problem is that your server is using a very old version of perl 5.005, so either it was not included with the distribution of perl you have or it was removed for some reason.
Thanks Kevin.

I read your previous article how to fix the problem on the module issue

Perl - read a line from a file and writing to the same line in the file

But still getting the error....

Anyway many many thanks for your time and given code. I'll try it.


Regards,
prady
  #13 (permalink)  
Old 03-31-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by KevinADC View Post
You could do something like this:


Code:
use strict;
use warnings;
my $flag = 0;
my $last_line;
while(<DATA>){
   if(/^\*Main Start/){
      $flag = 1;
   }
   elsif($flag && /^\*Main End/) {
         $numbers = (split(',',$last_line))[0];
         $last_line = '';
         $flag = 0; 
         print $numbers,"\n";
   }
   else {
      $last_line = $_;
      next;
   }
}	
__DATA__
*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,,,,,,,,

Yes Kevin . I am pretty close to my goal but one thing that I want the number (ie, 000005) when *Main Start pattern found .

Regards,
user_prady
  #14 (permalink)  
Old 03-31-2008
Registered User
 

Join Date: Jan 2008
Posts: 729
Quote:
Originally Posted by user_prady View Post
Yes Kevin . I am pretty close to my goal but one thing that I want the number (ie, 000005) when *Main Start pattern found .

Regards,
user_prady
With the sample data you posted, the code I posted does that, it prints 000005.
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl regex, perl shift, regex, shift, shift perl, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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 11:31 PM
Perl: Match a line with multiple search patterns Juha Shell Programming and Scripting 10 04-09-2008 02: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



All times are GMT -4. The time now is 11:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0