Hi Hendrik

Am Sonntag, 6. März 2005 16.36 schrieb Hendrik Maryns:
> Hi,
>
> [...] I took the
> file and tied it with Tie::File, in order to easily acces it.  This
> probably isn't all necessary here, but I want to modify the file itself,
> not writing the output to a new one.

The solution below does that, but without Tie::File, and scales well even for 
large files.

> The first thing is stripping of a date and time at the beginning of each
> line.  My re seems to be correct, as it works.  I do not understand why
> I need the /g modifier though.  

I don't know either...

> If I remove it, only the first line that 
> matches gets stripped.  I thought the substitution was started all over
> again for every line?
>
> Well, in writing this, I solved half of my problem, but one still
> remains: how can I remove a line?  I tried with delete, as you see
> below, but (of course) this does not work, as $lijn is no array element.

I think you also can't do it with an array (delete does not _remove_ an array 
element)

>   How can I totally remove that line from my file?
>
> This is my code:
>
> use strict;
> use warnings;
> use Tie::File;
>
> my @bestanden_lijst = <*.log>;
> for my $best (@bestanden_lijst){
>  tie my @bestand, 'Tie::File', $best or die "Kon het bestand niet
> binden: ", $!;
>  for my $lijn (@bestand){
>   $lijn =~ s{\[\d+/\d+/\d+\s\d+:\d+\]\s}{}g;
>   #delete $lijn if $lijn !~ /^<.*>/;
>  }
>  untie @bestand;
> }

For testing, I used simpler formatted files, other filenames, simpler 
regexes and (for better readability) minimal error messages 
- see #*# lines which I made simpler/different.

use strict;
use warnings;
use Tie::File;
my $tmp='tempfile';

#*# my @bestanden_lijst = <*.log>;
my @bestanden_lijst = <file*>;

foreach my $f (@bestanden_lijst) {
 open OLD, "<", $f or die $!;
 open NEW, ">", $tmp or die $!;

 while (<OLD>) {

   #*# $_=~s{\[\d+/\d+/\d+\s\d+:\d+\]\s}{};
   $_=~s{delete}{};

   #*# print NEW $_ if $_ !~ /^<.*>/;
   print NEW $_ if $_ !~ /^A/;

 }

 close OLD or die $!;
 close NEW or die $!;
 rename $f, $f.".original" or die $!;
 rename $tmp, $f or die $!;
}


The input files (file1, file2)
A
B
C
D
A
A delete
B delete
===
Ax
Bx
Cx
Dx
Ax
Ax delete
Bx delete


The output:
B
C
D
B
===
Bx
Cx
Dx
Bx


I hope that this gives you the help you wanted :-)

greetings joe


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to