Link to home
Start Free TrialLog in
Avatar of fabianope
fabianope

asked on

Text processing question

hello, the problemi is simple:

here's the input (.txt format):

##L
008L02C01---000225505
040L02C01---Agenti e rappresentanti
013L02C01---SEB Per 344.01
039L02C01---[SEB Per 344.01] ####2(2002)-
##L
008L02C01---000225469
040L02C01---Ambiente
013L02C01---SEB Per 344.046
039L02C01---[SEB Per 344.046] ####10(2002)-
##L
008L02C01---000186787
040L02C01---Ambiente & sicurezza
013L02C01---SEB Per 344.046
039L02C01---##2(2000)-  (SEB Per 344.046)
##L
008L02C01---000228903
040L02C01---Amministrazione civile
013L02C01---SEB Per 351
039L02C01---[SEB Per 351] ####2002-


and I'd like to obtain the following "polished" output:



Agenti e rappresentanti
SEB Per 344.01
2(2002)-


Ambiente
SEB Per 344.046
10(2002)-


Ambiente & sicurezza
SEB Per 344.046
2(2000)-  (SEB Per 344.046)


Amministrazione civile
SEB Per 351
2002-


is it possible?

thanks a lot,

Fabiano
Avatar of dda
dda
Flag of Russian Federation image

Very quick and dirty:
#!/usr/bin/perl -w

use strict;

while (my $s = <>) {
    chomp $s;
    if ($s =~ /##L/) {
        $s = <>;
        $s = <>;
     chomp $s;
        my @t = split /---/, $s;
        print $t[1], "\n";
        $s = <>;
     chomp $s;
        my @t = split /---/, $s;
        print $t[1], "\n";
        $s = <>;
        $s =~ /#+(.+)/;
        print $1, "\n\n";
    }
}
Run it as perl script.pl infile.txt > outfile.txt
Avatar of fabianope
fabianope

ASKER

hello,
thanks for the quick answer

with windows NT + active perl Build 515 Friday, April 9, 1999 I obtainde the followingf error from the DOS shell:


C:\prl>vedi.pl kris.txt > kris2.txt
"my" variable @t masks earlier declaration in same scope at C:\prl\vedi.pl line
15.
Use of uninitialized value at C:\prl\vedi.pl line 19, <> chunk 235.

If you can delete these errors, all goes OK

bye

fabianope
hello

another thing:

if I call the script as follows (in the worng manner..)


C:\prl>vedi.pl kris.txt

without output file, I obtain the right result on the STDOUT (the msdos shell)

hope it helps

bye

fabianope
ASKER CERTIFIED SOLUTION
Avatar of dda
dda
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,
this time the result is correct but I obtain a strange error message:

"my" variable @t masks earlier declaration in same scope at ved
Use of uninitialized value at vedi.pl line 19, <> chunk 235.

can you resolve this?
everyway thanks: I've accepted the answer.

bye

Fabianope
Yes, it's my fault. Please just remove second 'my' from tge script:

#!/usr/bin/perl -w

                   use strict;

                   while (my $s = <>) {
                      chomp $s;
                      if ($s =~ /##L/) {
                          $s = <>;
                          $s = <>;
                       chomp $s;
                          my @t = split /---/, $s;
                          print $t[1], "\n";
                          $s = <>;
                       chomp $s;
                          @t = split /---/, $s;
                          print $t[1], "\n";
                          $s = <>;
                          $s =~ /#+(.+)/;
                          print $1, "\n\n";
                      }
                   }

Regards,
Dmitry.