Re: regex headache

2014-02-04 Thread Dr.Ruud

On 2014-02-03 21:30, Paul Fontenot wrote:

Hi, I am attempting to write a regex but it is giving me a headache.

I have two log entries

1. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR
[org.apache.commons.logging.impl.Log4JLogger]
2. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR [STDERR]

I am using the following
^\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\w+\s+\[\d{1,2}:\s+\d{1,2}:\d{1,
2},\d{3}\]\s+\w+\s+\[[a-zA-Z0-9.]\]

My problem is this greedy little '.' - I need to just be a period. How do I
match #1 and not match #2?



I think you should replace \[[a-zA-Z0-9.]\] by \[[^]]+\].

Don't worry of matching, see this as parsing, and skip a line on how it 
matches, not on how it doesn't match.


Hint: start using named captures.

If you are into massively scanning log files, try MCE::Grep.

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Replacing the first line of a text file

2014-02-04 Thread Rob Dixon

On 04/02/2014 01:12, SSC_perl wrote:

use strict;
use warnings;

my $filename = 'products.csv';
open (my $fh, '+', $filename) || die can't open $filename: $!;
my $line = $fh;
$line =~ s/tag_//gi;
seek $fh, 0, 0;
printf {$fh} $line;
close $fh;



If it is satisfactory to leave the original file with trailing spaces at
the end of the first line, then you can do so like this

use strict;
use warnings;

my $filename = 'products.csv';
open (my $fh, '+', $filename) || die can't open $filename: $!;

chomp(my $line = $fh);
(my $new_line = $line) =~ s/tag_//gi;
my $pad_size = length($line) - length($new_line);

seek $fh, 0, 0;
printf {$fh} $line, ' ' x $pad_size;

close $fh or die $!;


or much more simply you could use the Tie::File module, like this

use strict;
use warnings;

use Tie::File;

my $filename = 'products.csv';

tie my @file, 'Tie::File', $filename or die can't open $filename: $!;
$file[0] =~ s/tag_//gi;
untie @file;


HTH,

Rob


---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Replacing the first line of a text file

2014-02-04 Thread Shawn H Corey
On Tue, 04 Feb 2014 11:59:30 +
Rob Dixon rob.di...@gmx.com wrote:

 If it is satisfactory to leave the original file with trailing spaces
 at the end of the first line, then you can do so like this
 
 use strict;
 use warnings;
 
 my $filename = 'products.csv';
 open (my $fh, '+', $filename) || die can't open $filename: $!;
 
 chomp(my $line = $fh);
 (my $new_line = $line) =~ s/tag_//gi;
 my $pad_size = length($line) - length($new_line);
 
 seek $fh, 0, 0;
 printf {$fh} $line, ' ' x $pad_size;

printf {$fh} $new_line, ' ' x $pad_size, \n;

 
 close $fh or die $!;



-- 
Don't stop where the ink does.
Shawn

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Replacing the first line of a text file

2014-02-04 Thread SSC_perl
Thanks everyone.  Both Tie::File and File::Slurp work nicely for this 
purpose.  Now comes the hard part - picking which one I want to use. ;)

Frank

http://www.surfshopcart.com/
Setting up shop has never been easier!



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/