Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Log File Extracting Multi Line Records

Status
Not open for further replies.

zenosan

Programmer
Jan 16, 2008
3
US
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:

#!/usr/bin/perl -w

foreach $file (@ARGV) {
open(fd1,"<$file");
$mydata = "";
while ($mydata = <fd1>) {
if ($mydata =~ /^.*MCBTS-1-MC1900BTS1183.*BTSCALLPROCESSING-1-BTSCALLPROCESSING1$/) {
print "$mydata";
$mydata = <fd1>;
until ($mydata =~ /^#END$/) {
print "$mydata";
$mydata = <fd1>;
}
print "$mydata";
}
}
close fd1;
}

Any help is appreciated.
 
Try out the following:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[olive][b]foreach[/b][/olive] [blue]$file[/blue] [red]([/red][blue]@ARGV[/blue][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$fh[/blue], [blue]$file[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red]

	[olive][b]while[/b][/olive] [red]([/red]<[blue]$fh[/blue]>[red])[/red] [red]{[/red]
		[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^.*MCBTS-1-MC1900BTS1183.*BTSCALLPROCESSING-1-BTSCALLPROCESSING1$[/purple][red]/[/red]..[red]/[/red][purple]^#END$[/purple][red]/[/red][red])[/red] [red]{[/red]
			[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url][red];[/red]
		[red]}[/red]
	[red]}[/red]
	close [blue]$fh[/blue][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
Thanks Miller,

Worked like a charm. Your code was a lot more succinct than mine.

Now, I would like to submit a range of values for the BTS ID in the if() line that would only pull the records for that range. For example:

MC1900BTS#### - MC1900BTS####

 
cross-posters, just helped him on TSDN and here he has the same question being replied to.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Whenever doing file processing, try to only have one place slurping from the file. Then use state variables if you want special functionality. The Range Operator is good for this type of logic, but it's not necessary to use it. If you're going to do anything more complicated, then I suggest that you separate the logic out like so. Just add your more complicated starting condition in teh appropriate if:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[olive][b]foreach[/b][/olive] [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] [red]([/red][blue]@ARGV[/blue][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$fh[/blue], [blue]$file[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red]

	[black][b]my[/b][/black] [blue]$matches[/blue] = [fuchsia]0[/fuchsia][red];[/red]
	[olive][b]while[/b][/olive] [red]([/red]<[blue]$fh[/blue]>[red])[/red] [red]{[/red]
		[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^.*MCBTS-1-MC1900BTS1183.*BTSCALLPROCESSING-1-BTSCALLPROCESSING1$[/purple][red]/[/red][red])[/red] [red]{[/red]
			[blue]$matches[/blue] = [fuchsia]1[/fuchsia][red];[/red]
		[red]}[/red]
		
		[olive][b]if[/b][/olive] [red]([/red][blue]$matches[/blue][red])[/red] [red]{[/red]
			[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url][red];[/red]
		[red]}[/red]
		
		[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^#END$[/purple][red]/[/red][red])[/red] [red]{[/red]
			[blue]$matches[/blue] = [fuchsia]0[/fuchsia][red];[/red]
		[red]}[/red]
	[red]}[/red]
	close [blue]$fh[/blue][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller

 
hmm, it seemed to me that I should be able to use this for a project of mine which is similiar but I'm not getting it.

I want to check a line to see if it starts with "400" and if it does , take the 3rd field and then all of the next line and append to a file.
 
Well, nobody here reads minds, post your code if you need help.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top