Solved

Text processing question

Posted on 2002-05-28
7
168 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:fabianope
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:dda
ID: 7038959
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";
    }
}
0
 
LVL 4

Expert Comment

by:dda
ID: 7038960
Run it as perl script.pl infile.txt > outfile.txt
0
 

Author Comment

by:fabianope
ID: 7041134
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:fabianope
ID: 7041152
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
0
 
LVL 4

Accepted Solution

by:
dda earned 200 total points
ID: 7041183
Please try to run as follows:

C:\prl>perl vedi.pl kris.txt > kris2.txt

I noticed that redirection may work incorrectly with winNT, and it is ok with w2k.
0
 

Author Comment

by:fabianope
ID: 7042104
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
0
 
LVL 4

Expert Comment

by:dda
ID: 7042404
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now