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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assistance editing a script to parse out some numbers... 1

Status
Not open for further replies.

CTSQLGUY

Technical User
Feb 4, 2007
33
US
Hello!

I have a script that needs some tweaking.

Right now, I have the script grabbing a number of different lines from one file and inserting them into another.

Now what I need is to be able to grab ONLY the last digits and insert them into my file.

Here is an example of what I need to parse:

--------------------------------------------
::= { sonicwallFwTrapRoot 0 2 }
--------------------------------------------

What I need is ONLY the last digit there, just the two, the other stuff I can discard.

Right now the last part of the script (which is what needs to be changed) is setup like this:

--------------------------------------------
my $trapid = '';
if ($record =~ /^\s+::= (\d+)/m) {
$trapid = $1;
} else {
print OUTFILE "Error: Trapid not found in record - $record";
}
--------------------------------------------

What has to be changed so I can have it ONLY grab the last block? Keep in mind, the last block of numbers will change in size, it wont always be a single digit.

In the example above the last block I'm referencing is 2.

Thanks for the help!
 
Try this. I didn't test it but it should grab the last set of digits given the string has the closing curly bracket.

$record =~ /(\d+)\s*\}\s*$/m
 
Take advantage of greedy matching in order to get to the "last" number, and then simply make sure to indicate a boundary in order to ensure that you get the entire number.

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

[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$record[/blue] = [blue]$_[/blue][red];[/red]
	
	[black][b]my[/b][/black] [blue]$trapid[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$record[/blue] =~ [red]/[/red][purple]^[purple][b]\s[/b][/purple]+::=.*[purple][b]\b[/b][/purple]([purple][b]\d[/b][/purple]+)[/purple][red]/[/red][red])[/red] [red]{[/red]
		[blue]$trapid[/blue] = [blue]$1[/blue][red];[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Found: [blue]$trapid[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [red]"[/red][purple]Error: Trapid not found in record - '[blue]$record[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]    ::= { sonicwallFwTrapRoot 0 2 }[/teal]
[teal]    ::= { no number }[/teal]
[teal]    ::= { long number 1000 }[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
Miller - you've done it again!

Thanks a ton everyone for your help - it's greatly appreciated! :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top