Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print a table 1

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
I have a script that checks the available disk space on a few servers. It calculates the total drive capacity, free space, used space, used %, and free %. I want to be able to print this out in table format so that it looks like this...

Server Drive Total Used Free %Used %Free
ServerA C 60.0 30.0 30.0 50% 50%

Each server is listed in an array and a foreach loop goes to each one and calculates each of the above. How can I make this print in a neat, table format like above? I basically want to say "print $Used on the 32nd space and then print $Free on the 44th space", for example.

Thanks,
Chris
 
try:
perldoc perlform

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Ok, that sounds like its what I need. I took the example from the man page, copy and pasted it and change the variables but it doesn't print anything.

Code:
#!/usr/local/bin/perl

$name = 'chris';
$login = 'chrisp';
$office = 'NY';
$uid = '500';
$gid = '500';
$home = '/home/chrisp';

# a report on the /etc/passwd file
format STDOUT_TOP =
                         Passwd File
Name                Login    Office   Uid   Gid Home
------------------------------------------------------------------
.
format STDOUT =
@<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
$name,              $login,  $office,$uid,$gid, $home
.

What is wrong with this code?

Thanks,
Chris
 
Add this line to the end of your code:
Code:
write;

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
To make it clear, everytime you perform a write the format is provoked with the values available at that moment.
 
...one more question, instead of printing this to STDOUT, I want to email this info. I'm thinking that the best way would be to print everthing to a local text file, and then open that later and mail the contents. I don't see how you can print those 'format' lines to a text file.

Thanks
 
That doesn't seem to work...

Code:
$file = 'C:\driveinfo.txt';
open(FILE, ">$file") or die "Can't open $file: $!\n";
write (FILE);

I get this error when I run the script...

Undefined format "FILE" called at C:\driveinfo.pl line 39, <DATA> line 164.

Thanks,
Chris
 
You need to create a format for the FILE filehandle. Look in your code and you will see where you format STDOUT_TOP and format STDOUT. Change these to format FILE_TOP and format FILE.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
I changed them and am getting this error...

Undefined format "STDOUT" called at C:\driveinfo.pl line 70, <DATA> line 164.

Line 70 simply says 'write;' on it. I changed 'format STDOUT_TOP = ' to 'format FILE_TOP = ' and 'format STDOUT = ' to 'format FILE = '.

Thanks,
Chris
 
If you are going to be writing to both, then you will need to define both. Check the docs, there may be a way to assign a format to two handles. Or it may work to join the two I don't know. I do know that it should work to define both.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
If you are going to be writing to both, then you will need to define both. Check the docs, there may be a way to assign a format to two handles. Or it may work to join the two I don't know. I do know that it should work to define both.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
If you take this very example as an entire script, it still gives you that same error...

Code:
#!perl

$file = 'C:\driveinfo.txt';
open(FILE, ">$file") or die "Can't open $file: $!\n";

format FILE =
Server     Drive  Total    Used     Free     %Used    %Free
-----------------------------------------------------------
.
write;

Undefined format "STDOUT" called at C:\test.pl line 10.

Chris
 
...nevermind, I got it...

format FILE_TOP =
blablabla
format FILE =
blablabla
write (FILE)



Thanks,
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top