tags:

views:

107

answers:

3

I got a text file of this kind

INFO [main] (porter.java:100) - Added record 7147  read from file: 1484301                 
INFO [main] (porter.java:100) - Added record 7148  read from file: 1484302   
INFO [main] (porter.java:100) - Added record 17147 read from file: 1484303  
INFO [main] (porter.java:100) - Added record 76148 read from file: 1484333  
INFO [main] (porter.java:100) - Added record 148   read from file: 1484342

How can I parse this, getting the first digits in this case like

7147
7148
17147
76148
148
+3  A: 

A regex would work nicely. Depending on what else is in the file you could get away with

while (<>) {
    next unless my ($n) = /Added record ([0-9]+)/;
    print "$n\n";
} 
Chas. Owens
`if ( defined $n ) { print "$n\n" }`
ysth
my $string = "INFO [main] (porter.java:100) - Added record 7147 read from file: 1484301"; How can i get 7147 from this.Sorry chris i'm new to perl and im learning.
Sunny
`my ($n) = $string =~ /Added record ([0-9]+)/;` Read [`perldoc perlretut`](http://perldoc.perl.org/perlretut.html).
Chas. Owens
+1  A: 

I was thinking of the following pattern for the regex:

my ($n) = /record ([0-9]+)/;

This might capture lines containing Deleted record | Appended record | Changed record | etc. etc..

Ibrahim
A: 

From the shell:

perl -wlane'print $F[6]' <infile >outfile

or

cut -d ' ' -f 7 <infile >outfile
ysth