Link to home
Start Free TrialLog in
Avatar of d_asselin
d_asselinFlag for Canada

asked on

Perl script question

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

Avatar of Carl Bohman
Carl Bohman
Flag of United States of America image

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?
Avatar of d_asselin

ASKER

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

 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

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


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?

 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

ASKER CERTIFIED SOLUTION
Avatar of Carl Bohman
Carl Bohman
Flag of United States of America 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