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

Extract from a text file

Status
Not open for further replies.

CarlSe

Technical User
Nov 12, 2008
2
SE
Hi!

I've used sed to extract a specific part from a line from a logfile. However, I would like to use perl to do the same instead, so I would appreciate some feedback.

I'm using perl on a Windows 2k3 server for this purpose right now. But it will later on also be used on a Linux-server as well.

The file is a simple text-file containing anything from 5 - 50 lines of text. A new logfile is created on a daily basis.

There's only one line somewhere in that logfile, which contain the text: .... svn: ##### ...where ##### is a unique number. So it can look like svn: 79352 or svn: 26482 etc. And it's somewhere in the middle of some of the lines, but never at the same place.

I want to extract the 5 digits to a string variable.

The code should be as short as possible.

Thanks in advance :)

 
Hi

Code:
#! /usr/bin/perl
use strict;

my ($svn_text,$svn_number);
my @lines = ('line1','line2','svn: 98765','line4','line5');
my $svn_initial = 'svn: ';
foreach (@lines) {
	if ($_ =~ /^$svn_initial[0-9]{5,5}$/) {
		($svn_text,$svn_number) = split(/\:/,$_);
	}
}
if ($svn_number) {
	print "SVN NUMBER = $svn_number";
}
else {
	print "SVN NUMBER not found";
}

The code above assumes that svn only occurs once, otherwise the last svn found is used. The svn number must be 5 digits long. To read the data from a text file into an array you would use something like:

Code:
open (LOG, "</path/to/file");
my @lines = <LOG>;
close (LOG);

Chris
 
Just whilst i'm thinking about it...

You could replace:
($svn_text,$svn_number) = split(/\:/,$_);

With:
$_ =~ s/^$svn_initial(.+)$/$1/;
$svn_number = $_;

Does the same thing, I suppose its down to personal choice.

Chris
 
A little shorter:

Code:
my $num = 'not found';
open (LOG, "</path/to/log") or die "$!";
while(<LOG>) {
   last if (($num) = /svn:\s*(\d+)/);
}
close (LOG);
print $num;


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Very nice! Quick question @Kevin,

If a svn number isn't found, it won't print 'not found'. Any reason why? Also how would you personally ensure that the number if 5 digits long (I would use [0-9]{5,5})? Thanks,

Chris
 
If you want only five digits you need to add something after the digits in the search pattern, in this example I added a space and change where 'not found' gets defined:

Code:
my $num;
open (LOG, "</path/to/log") or die "$!";
while(<LOG>) {
   last if (($num) = /svn:\s*(\d{5})\s/);
}
close (LOG);
$num ||= 'not found';
print $num;

\d is the same as [0-9]

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

Part and Inventory Search

Sponsor

Back
Top