Solved

Perl script question

Posted on 2009-06-30
9
246 Views
Last Modified: 2012-05-07
Hi

Some of you may remember this perl code
From earlier this week.  Well it works fine
The reason for which it wasnt working
Was one of the input file. In order to correct
The problem I had to pass that file true filter
Like so  
gawk -F"|" '{print $1","$2,",",$3,",",$11 >> "new.file"}' patchdiag.xref

below is the script I received from you
and another  in perl that dose the same thing
I tried to imbed one with the other but failed
Miserably.

I can script in BASH pretty good but I,m just starting perl
So how dose one go about doing that, And please this
Time show me rather than doing it or explain if you can
Thank you

Code 1
#!/usr/local/bin/perl -w
use strict;
 
die "Usage: $0 <file1> <file2> <file_out>\n" unless $#ARGV==2;
my ($file1, $file2, $file3) = @ARGV;
 
open my $f1_in, $file1 or die "Could not open $file1\n";
open my $f2_in, $file2 or die "Could not open $file2\n";
open(my $f3_out, '>', $file3) or die "Could not open $file3: $!\n";
 
while  (my $f1 = <$f1_in>) {
    my $f2 = <$f2_in>;
    $f1 =~ s/^\s+|\s+$//g;
    $f2 =~ s/^\s+|\s+$//g;
 
    my @f1 = split(/\-/, $f1);
    my @f2 = split(/\,/, $f2);
    print $f3_out join(" ", @f1[0..1], @f2[1..$#f2])."\n";
}
 
code 2
open(NEW_FILE, '>>new-file') || die 'Cannot create file "new-file".';
 
$\ = "\n";
$FS = '|';
$, = ',';
 
while (<>) {
    ($Fld1,$Fld2,$Fld3,$Fld4,$Fld5,$Fld6,$Fld7,$Fld8,$Fld9,$Fld10,$Fld11) = split(/[|\n]/, $_
, -1);
    print NEW_FILE $Fld1, $Fld2, $Fld3, $Fld11;
}

Open in new window

0
Comment
Question by:d_asselin
[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
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24749091
What exactly are you trying to accomplish?  One script takes three files and merges data from the first two into the third file.  The other takes default input (piped, stdin, or the contents of a named file--via <>) and appends portions of the input it to a specific file.  In the end, what inputs do you want to supply to it?  What outputs are you looking for?
0
 

Author Comment

by:d_asselin
ID: 24749363
What Im trying to achieve;  is this
The first code you see takes file1 matches it to file2
Using the first filed of each file and writes the output
To file3.  Now that works perfectly

 The problem is with file2 in it there are records to long
And the script cannot Handel them  so I had to filter out
What I needed and make file 2 manageble

I did this to prove that is was the problem that is why
You see the gawk command string

If possible I would like to imbed that in the first code
In other words filter file to in the perlscript
 
0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24754794
Correct me if I am wrong, but it sounds like file2 has too many fields in each line and you just want a subset of fields printed.  Specifically, you want only the 1st, 2nd, 3rd, and 11th fields printed to file3.  Assuming that is what you are looking for, replace line 19 in your code to the attached code.

Note that the indexes in my code are reduced by one because the 1st field has an index of 0, 2nd has an index of 1, etc.  Your original code was dropping the first field completely.
print $f3_out join(" ", @f1[0..1], @f2[0,1,2,10])."\n";

Open in new window

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:d_asselin
ID: 24755432
 Hi you are correct the fields that need are  1 2 3  11
   But the file2 is read integrally by perl and the and
Some of the fields in this file  are 9000 characters long
 Vi, ed, awk, bailout with the error message line to long
Only gawk works it doesnt have the limitation and
The initial perl script also

But I did try you line
   
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 1.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 2.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 3.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 4.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 5.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 6.

#!/usr/local/bin/perl -w
use strict;
 
die "Usage: $0 <file1> <file2> <file_out>\n" unless $#ARGV==2;
my ($file1, $file2, $file3) = @ARGV;
 
open my $f1_in, $file1 or die "Could not open $file1\n";
open my $f2_in, $file2 or die "Could not open $file2\n";
open(my $f3_out, '>', $file3) or die "Could not open $file3: $!\n";
 
while  (my $f1 = <$f1_in>) {
    my $f2 = <$f2_in>;
    $f1 =~ s/^\s+|\s+$//g;
    $f2 =~ s/^\s+|\s+$//g;
 
    my @f1 = split(/\-/, $f1);
    my @f2 = split(/\,/, $f2);
#    print $f3_out join(" ", @f1[0..1], @f2[1..$#f2])."\n";
print $f3_out join(" ", @f1[0..1], @f2[0,1,2,11])."\n";
}

Open in new window

0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24755834
You used 0,1,2,11, not 0,1,2,10.  I'm guessing there are a total of 11 fields in your data, which means that index 10 is the maximum index.  That likely explains the "uninitialized value" warnings and may explain any missing data.
0
 

Author Comment

by:d_asselin
ID: 24756704
No there are 10 fields

                  here is a extract of file1
118666-20
118777-14
119059-47
120272-24
121118-16
122212-33
123938-02
137080-03

          and here is file2100287|05|Oct/31/91| | | |  |Unbundled|||PC-NFS 3.5c: Jumbo patch (updated PRT.COM to v3.5c)
100323|05|Feb/11/92| | | |  |Unbundled|||PC-NFS Advanced Telnet: bug fixes, National Character Set support
100386|01|Sep/20/91| | | |  |Unbundled|||PC-NFS Programmer's Toolkit/2.0: Runtime modules
100393|01|Sep/02/94| | |O|  |Unbundled|||OBSOLETED by 100394
100438|01|Dec/02/91| | | |  |Unbundled|||PCNFS/v3.5c: PC-NFS loses mounted printer names when PCNFSLPD load
100455|01|Dec/17/91| | | |  |Unbundled|||LifeLine/2.0: Does not parse the FROM: and/or Reply-TO: fields cor
100648|01|Jun/11/92| | | |  |Unbundled|||PC-NFS/4.0a: Jumbo Patch
100701|03|Feb/17/93| | | |  |Unbundled|sparc;|SUNWowbcp:1.8.1;SUNWowbcp:2.10;|OpenWindows 3.0.1;3.1: program
s compiled under Solaris 1.x /usr/5b
100791|04|Apr/12/93| | |O|  |Unbundled||SPROsw:2.1.0.1;|OBSOLETED by 100974
100807|03|May/02/94| | |O| B|Unbundled|||OBSOLETED by WITHDRAWN
100807|04|Jul/07/94| | | |  |Unbundled|sparc;|SPROswmgr:2.1.2.1;|Sparcworks 2.0.1: statically linking X11 li
brary doesn't run on As


0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24756953
100791|04|Apr/12/93| | |O| |Unbundled||SPROsw:2.1.0.1;|OBSOLETED by 100974
0: 100791
1: 04
2: Apr/12/93
3: (a single space)
4: (a single space)
5: O
6: (two spaces)
7: Unbundled
8: (empty field)
9: SPROsw:2.1.0.1;
10: OBSOLETED by 100974

That's 11 fields (with indexes 0-10 once they are placed in the @f2 array). Referencing $f2[11] will result in an undefined value warning.

As per my previous comment, you need to change your code from
@f2[0,1,2,11]
to
@f2[0,1,2,10]
After that change, are you still getting problems generating the resulting file?  Or is the problem only in viewing the resulting file?
0
 

Author Comment

by:d_asselin
ID: 24757369

 Hummm....

Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 1.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 1.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 1.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 2.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 2.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 2.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 3.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 3.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 3.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 4.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 4.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 4.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 5.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 5.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 6.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 6.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 6.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 7.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 7.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 7.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 8.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 8.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 8.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 9.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 9.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 9.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 10.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 10.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 10.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 11.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 11.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 12.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 12.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 12.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 13.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 13.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 13.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 14.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 14.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 14.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 15.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 15.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 15.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 16.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 16.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 16.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 17.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 17.
Use of uninitialized value in join or string at ./sortit.pl line 19, <$f2_in> line 17.
<>root@cdpqda: ~#
#!/usr/local/bin/perl -w
use strict;
 
die "Usage: $0 <file1> <file2> <file_out>\n" unless $#ARGV==2;
my ($file1, $file2, $file3) = @ARGV;
 
open my $f1_in, $file1 or die "Could not open $file1\n";
open my $f2_in, $file2 or die "Could not open $file2\n";
open(my $f3_out, '>', $file3) or die "Could not open $file3: $!\n";
 
while  (my $f1 = <$f1_in>) {
    my $f2 = <$f2_in>;
    $f1 =~ s/^\s+|\s+$//g;
    $f2 =~ s/^\s+|\s+$//g;
 
    my @f1 = split(/\-/, $f1);
    my @f2 = split(/\,/, $f2);
#    print $f3_out join(" ", @f1[0..1], @f2[1..$#f2])."\n";
print $f3_out join(" ", @f1[0..1], @f2[0,1,2,10])."\n";
}

Open in new window

0
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 500 total points
ID: 24757490
I just realized that you are splitting on comma on line 17, not by vertical bar (which is the format your file is in), change line 17 to the following.
my @f2 = split(/\|/, $f2);

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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

691 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