Hello guys,
I've a file (with a lot of string lines of this format .*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?(?:$)); I want to check those who start with the same first word after the first semicolon, then delete one of them from the file (the one that have a particular format).
I write this code:
#!/usr/bin/perl
use warnings;
my $input = 'lines.txt';
open (FILE, "<", $input) or die "Can not open $input $!";
my @lines = <FILE>;
close FILE;
my @forlines=();
foreach(@lines){
if( ( defined $_) and !($_ =~ /^\s*$/)){
push(@forlines, $_);
}
}
my @buffer = ();
foreach $line(@forlines)
{
$line =~/(.*?);.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?(?:$)/;
my $var = $1;
foreach $line2(@forlines)
{
$line2 =~/(.*?);.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?(?:
+$)/;
if ($line2 eq $line)
{
next;
}
if($var eq $1 and $line2 =~ /(.*?);.*?;SU LI IR ST;.*?
+;SU LI IR ST;.*?;.*?;.*?;.*?;.*?(?:$)/)
{
push @buffer, $line2;
}
}
}
foreach $line(@buffer)
{
@forlines = grep {!/$line/} @forlines;
}
open(my $file, '>', $input) or die "Could not open file '$input' $!";
truncate $file,0;
print $file @forlines;
it works but it is so slow (20 min to formating the file), I think because of the two nested loops.
Is there any better solution to do that ?
BR
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.