Operating System - HP-UX
1753684 Members
5570 Online
108799 Solutions
New Discussion юеВ

Spaces in file causing problems with my array

 
SOLVED
Go to solution
Steve_617
Advisor

Spaces in file causing problems with my array

I wrote the following perl scripts but when there are multiple spaces it does not perform the way I though it should. The script looks like this
#!/usr/bin/perl -w
`rm /tmp/tmp1/*`;
`cat /home1/reports/output/ljhbdss1/tmpftp >/tmp/tmp1/tmpfile`;
`cat /home1/reports/output/lptadss1/tmpftp >>/tmp/tmp1/tmpfile`;
`cat /home1/reports/output/lblmdss1/tmpftp >>/tmp/tmp1/tmpfile`;
`cat /home1/reports/output/lctndss1/tmpftp >>/tmp/tmp1/tmpfile`;
`cat /home1/reports/output/lpthdss1/tmpftp >>/tmp/tmp1/tmpfile`;
`cat /home1/reports/output/ldbndss1/tmpftp >>/tmp/tmp1/tmpfile`;

open (FOA, " > /tmp/tmp1/tmp.avg");
open (FOT, " > /tmp/tmp1/tmp.det");
open (FI, "< /tmp/tmp1/tmpfile") or die "Cannot find ljhbdss1 tmp file";
while ($line=)
{
@LIST=(split/ /,($line));
chomp @LIST;
$var=$LIST[1];
print "creating $var\n";
sleep 1;
open (FN, ">> /tmp/tmp1/$var");
print FN @LIST,"\n";
close FN;
}
close FI;
close FOA;
close FOT;
exit


The input files has the following entries.
13.04.2005 r141DMZ1 10.203.63.6 100
14.04.2005 r141DMZ1 10.203.63.6 100
15.04.2005 r141DMZ1 10.203.63.6 100
16.04.2005 r141DMZ1 10.203.63.6 100
17.04.2005 r141DMZ1 10.203.63.6 100
18.04.2005 r141DMZ1 10.203.63.6 100
19.04.2005 r141DMZ1 10.203.63.6 100
20.04.2005 r141DMZ1 10.203.63.6 100
20.04.2005 r141DMZ1 10.203.63.6 100
r141DMZ1 Month_Availability 100.00

It bombs when reading the Month_Availability.

Can anyone show me how to eliminate the double spaces or ignore them preferably.

Regards
12 REPLIES 12
harry d brown jr
Honored Contributor

Re: Spaces in file causing problems with my array

tr -s "XX" "X"

REPLACE the X's with same number of SPACES

live free or die
harry d brown jr
Live Free or Die
Steve_617
Advisor

Re: Spaces in file causing problems with my array

Do I do that as part of defining the array , or after defining the array.
IE
@LIST=(split/ /,($line));
tr / / /;
harry d brown jr
Honored Contributor
Solution

Re: Spaces in file causing problems with my array

Nope,

open (FI, 'cat /tmp/tmp1/tmpfile|tr -s "XX" "X"|') or die "Cannot find ljhbdss1 tmp file";

live free or die
harry d brown jr
Live Free or Die
Peter Godron
Honored Contributor

Re: Spaces in file causing problems with my array

Steve,
where are the double spaces in your input data file?

From what I see:
read the input file
append the record to a file named as the second field

This should create 1 file called r141DMZ1 with 9 records and 1 file called Month_Availability with 1 record.
Is this correct?

Regards
Steve_617
Advisor

Re: Spaces in file causing problems with my array

Yes this is correct
But the field
r141DMZ1 Month_Availability 100.00
has spaces in between and at the end after the 100.00, causing me more greay hairs.
So it looks like this
20.04.2005 r141DMZ1 10.203.63.6 100
20.04.2005 r141DMZ1 10.203.63.6 100
r141DMZ1 Month_Availability 100.00

I would like to replace all the spaces regardless of how much with just one and obviously no spaces at the end.
Steve_617
Advisor

Re: Spaces in file causing problems with my array

I see the view removes the spaces so I will display the unwanted spaces with x's
19.04.2005 r141DMZ1 10.203.63.6 100
20.04.2005 r141DMZ1 10.203.63.6 100
20.04.2005 r141DMZ1 10.203.63.6 100
r141DMZ1 xxxMonth_Availability xxx100.00xxxx

Regards

Peter Godron
Honored Contributor

Re: Spaces in file causing problems with my array

Steve,
thanks for the details.
Can you replace the
@LIST=(split/ /,($line));
with
@LIST=(split/\s+/,($line));
and try again.

But it seems you are already happy with Harry's fix.

Regards
H.Merijn Brand (procura
Honored Contributor

Re: Spaces in file causing problems with my array

Now first let's make it more perlish

--8<---
#!/usr/bin/perl

use strict;
use warnings;

unlink ;

# Use magic open
@ARGV = map { "/home1/reports/output/$_/tmpftp" } qw( ljhbdss1 lptadss1 lblmdss1 lctndss1 lpthdss1 ldbndss1 );
while (<>) {
chomp (my @LIST = split /\s+/, $_, -1);
$var=$LIST[1];
print "creating $var\n";
sleep 1;
open my $fn, ">> /tmp/tmp1/$var" or die "$var: $!";
print $fn @LIST, "\n";
close $fn;
}
-->8---

Much more readable IMHO

I removed FOA and FOT, because they're not used in the snippet

It also splits on one or more whitespace characters

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steve_617
Advisor

Re: Spaces in file causing problems with my array

Thought it was working but I am getting the following error message
Global symbol "$var" requires explicit package name at ./DayWeb.pl line 12.
Global symbol "$var" requires explicit package name at ./DayWeb.pl line 13.
Global symbol "$var" requires explicit package name at ./DayWeb.pl line 15.
Global symbol "$var" requires explicit package name at ./DayWeb.pl line 15.
Execution of ./DayWeb.pl aborted due to compilation errors.
$