Solved

Text processing question

Posted on 2002-05-28
7
167 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

17 Experts available now in Live!

Get 1:1 Help Now