writing array to file?
am 21.08.2007 18:40:57 von elroyerni
Hi -
I have a array that i initialize for using this in my perl script:
my @array_data = qx(cat /home/dchun/logs/$date/stat.log|grep "System
took"|cut -c 56-62);
I know this is really simple but I can't seem to figure it out. How do
i write this array to a text file in my home directory? I just want to
save it for later. If the file already exists, i just want to write
over it.
I tried using a system command.. but that didn't work. Any suggestions
on a simple way to do this?
system `cat /home/ssi9gwy/logs/$date/rco.log|grep "OTS took"|cut -c
56-62 > /u/dchun/rco_stats/$date.rco_stat.log`;
Re: writing array to file?
am 21.08.2007 18:48:32 von Paul Lalli
On Aug 21, 12:40 pm, elroyerni wrote:
> I have a array that i initialize for using this in my perl script:
> my @array_data = qx(cat /home/dchun/logs/$date/stat.log|grep "System
> took"|cut -c 56-62);
Okay... if you wanted to just do that in Perl rather than shelling out
to three system commands, it would be:
open my $fh, '<', "/home/dchun/logs/$date/stat.log" or die $!;
my @array_data;
while (my $line = <$fh>) {
if ($line =~ /System took/) {
push @array_data, substr($line, 55, 6);
}
}
>
> I know this is really simple but I can't seem to figure it out.
> How do i write this array to a text file in my home directory? I
> just want to save it for later. If the file already exists, i
> just want to write over it.
Open the file for writing, print the array to the file.
open my $ofh, '>', 'file_to_write.txt' or die $!;
for my $line (@array_data) {
print $ofh "$line\n";
}
> I tried using a system command..
You seem unusually dependent on shelling out to external programs.
Perl is more than a shell scripting language. You should take some
time to explore the rest of its features. I suggest you read:
perldoc -f open
perldoc perlopentut
perldoc -f print
> but that didn't work. Any suggestions
> on a simple way to do this?
>
> system `cat /home/ssi9gwy/logs/$date/rco.log|grep "OTS took"|cut -c
> 56-62 > /u/dchun/rco_stats/$date.rco_stat.log`;
This says to run the command between ``, and then attempt to execute
that command's *output*. backticks execute a command and return the
output. system() executes a command and returns the return value.
Paul Lalli
Re: writing array to file?
am 21.08.2007 19:02:33 von Jim Gibson
In article <1187714457.955337.306830@x35g2000prf.googlegroups.com>,
elroyerni wrote:
> Hi -
>
> I have a array that i initialize for using this in my perl script:
> my @array_data = qx(cat /home/dchun/logs/$date/stat.log|grep "System
> took"|cut -c 56-62);
>
> I know this is really simple but I can't seem to figure it out. How do
> i write this array to a text file in my home directory? I just want to
> save it for later. If the file already exists, i just want to write
> over it.
>
> I tried using a system command.. but that didn't work. Any suggestions
> on a simple way to do this?
>
> system `cat /home/ssi9gwy/logs/$date/rco.log|grep "OTS took"|cut -c
> 56-62 > /u/dchun/rco_stats/$date.rco_stat.log`;
>
Perl has input and output facilities for reading and writing files,
regular expressions for searching for patterns in strings, and
functions for manipulating strings. You should consider using them
(untested):
#!/usr/local/bin/perl
#
use warnings;
use strict;
my $date = '???';
my $infile = "/home/ssi9gwy/logs/$date/rco.log";
my $outfile = "/u/dchun/rco_stats/$date.rco_stat.log";
open(my $in, '<', $infile) or die("Can't open $infile: $!");
open(my $out, '>', $outfile) or die("Can't open $outfile: $!");
while(<$in>) {
next unless /OTS took/;
print $out substr($_,56,7), "\n";
}
close($in) or die("Error closing $infile: $!");
close($out) or die("Error closing $outfile: $!");
__END__
See
perldoc -f open
perldoc -f substr
perldoc perlre
perldoc perlop (search for 'I/O Operators')
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: writing array to file?
am 21.08.2007 20:15:04 von elroyerni
On Aug 21, 12:02 pm, Jim Gibson wrote:
> In article <1187714457.955337.306...@x35g2000prf.googlegroups.com>,
>
>
>
> elroyerni wrote:
> > Hi -
>
> > I have a array that i initialize for using this in my perl script:
> > my @array_data = qx(cat /home/dchun/logs/$date/stat.log|grep "System
> > took"|cut -c 56-62);
>
> > I know this is really simple but I can't seem to figure it out. How do
> > i write this array to a text file in my home directory? I just want to
> > save it for later. If the file already exists, i just want to write
> > over it.
>
> > I tried using a system command.. but that didn't work. Any suggestions
> > on a simple way to do this?
>
> > system `cat /home/ssi9gwy/logs/$date/rco.log|grep "OTS took"|cut -c
> > 56-62 > /u/dchun/rco_stats/$date.rco_stat.log`;
>
> Perl has input and output facilities for reading and writing files,
> regular expressions for searching for patterns in strings, and
> functions for manipulating strings. You should consider using them
> (untested):
>
> #!/usr/local/bin/perl
> #
> use warnings;
> use strict;
> my $date = '???';
> my $infile = "/home/ssi9gwy/logs/$date/rco.log";
> my $outfile = "/u/dchun/rco_stats/$date.rco_stat.log";
> open(my $in, '<', $infile) or die("Can't open $infile: $!");
> open(my $out, '>', $outfile) or die("Can't open $outfile: $!");
> while(<$in>) {
> next unless /OTS took/;
> print $out substr($_,56,7), "\n";}
>
> close($in) or die("Error closing $infile: $!");
> close($out) or die("Error closing $outfile: $!");
> __END__
>
> See
> perldoc -f open
> perldoc -f substr
> perldoc perlre
> perldoc perlop (search for 'I/O Operators')
>
> --
> Jim Gibson
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com
Thanks everyone for your help. As you can see I'm new to perl and am
very thankful for all your help and feedback as I try to master this
language.