By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 2,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Log File Extracting Multi Line Records

P: 1
SunOS bsm1b 5.9 Generic_118558-09 sun4u sparc SUNW,Netra-440

This is perl, v5.6.1 built for sun4-solaris

I have listed the platform/OS information for your reference.

I need to extract records from a log file with the following format:

|O%:CCLN-1-CBS1:CELLS-1-CELLS1:MCBTS-1-MC1900BTS1183
line 1
line 2
line 3
. . .
#End

There are several of these logs within the log file and I need them all. There is a distinct header and footer for each log as outlined above.

I created the following script, but it only prints one record:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. foreach $file (@ARGV) {
  4.   open(fd1,"<$file");
  5.   $mydata = "";
  6.   while ($mydata = <fd1>) {
  7.     if ($mydata =~ /^.*MCBTS-1-MC1900BTS1183.*BTSCALLPROCESSING-1-BTSCALLPROCESSING1$/) {
  8.         print "$mydata";
  9.         $mydata = <fd1>;
  10.         until ($mydata =~ /^#END$/) {
  11.           print "$mydata";
  12.           $mydata = <fd1>;
  13.         }
  14.        print "$mydata";
  15.      }
  16.   }
  17.   close fd1;
  18. }
  19.  
Any help is appreciated.
Jan 16 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
SunOS bsm1b 5.9 Generic_118558-09 sun4u sparc SUNW,Netra-440

This is perl, v5.6.1 built for sun4-solaris

I have listed the platform/OS information for your reference.

I need to extract records from a log file with the following format:

/|O%:CCLN-1-CBS1:CELLS-1-CELLS1:MCBTS-1-MC1900BTS1183
line 1
line 2
line 3
. . .
#End

There are several of these logs within the log file and I need them all. There is a distinct header and footer for each log as outlined above.

I created the following script, but it only prints one record:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. foreach $file (@ARGV) {
  4.   open(fd1,"<$file");
  5.   $mydata = "";
  6.   while ($mydata = <fd1>) {
  7.     if ($mydata =~ /^.*MCBTS-1-MC1900BTS1183.*BTSCALLPROCESSING-1-BTSCALLPROCESSING1$/) {
  8.         print "$mydata";
  9.         $mydata = <fd1>;
  10.         until ($mydata =~ /^#END$/) {
  11.           print "$mydata";
  12.           $mydata = <fd1>;
  13.         }
  14.        print "$mydata";
  15.      }
  16.   }
  17.   close fd1;
  18. }
  19.  
Any help is appreciated.
Hard to say, the pattern you are searching:

/^.*MCBTS-1-MC1900BTS1183.*BTSCALLPROCESSING-1-BTSCALLPROCESSING1$/

look almost nothing like the sample data you posted:

|O%:CCLN-1-CBS1:CELLS-1-CELLS1:MCBTS-1-MC1900BTS1183

Do you need to use more patterns or change the one you are using? Clarify what you are looking for.
Jan 17 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Heres a general Idea:

Expand|Select|Wrap|Line Numbers
  1. LOOP:  while ($mydata = <DATA>) {
  2.     if ($mydata =~ /^\Q|O%:CCLN-1-CBS1:CELLS-1-CELLS1:MCBTS-1-MC1900BTS1183\E$/) {
  3.         print "$mydata";
  4.         while ($mydata = <DATA>) {
  5.           print "$mydata";
  6.           next LOOP if $mydata =~ /#End/;
  7.         }
  8.      }
  9.   }
  10.  
  11. __DATA__
  12. |O%:CCLN-1-CBS1:CELLS-1-CELLS1:MCBTS-1-MC1900BTS1183
  13. line 1
  14. line 2
  15. line 3
  16. . . .
  17. #End
  18.  
  19. line should not print
  20. line should not print
  21. line should not print
  22. line should not print
  23.  
  24. |O%:CCLN-1-CBS1:CELLS-1-CELLS1:MCBTS-1-MC1900BTS1183
  25. line 1
  26. line 2
  27. line 3
  28. . . .
  29. #End
  30.  
  31. line should not print
  32. line should not print
  33. line should not print
Jan 17 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.