?
Solved

Text processing question

Posted on 2002-05-28
7
Medium Priority
?
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 800 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans
Suggested Courses

762 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