Hi,i was a beginner in perl .please try to help m ein these areas.
If i use

du -sh

in my perl script i was getting output as
54M /home/abhinav/compare or 569M /home/abhinav/debug

But i want only the size that is either like 54m or 569m and i dont want the path
CAN u help me for retrieving only size


if i use

df /compare/

The output has all the below.
Filesystem 1K-blocks Used Available Use% Mounted on
xhd-filer2:/vol/vol7/sw_apps
506854848 425468096 81386752 84% /proj/sw_apps

but i want only availablespace and used percent into two variables...

Please try to help me

your question isnt very clear....but, i'll have a go.

so, you have written a perl script and at some point you get this into a variable:
"xhd-filer2:/vol/vol7/sw_apps 506854848 425468096 81386752 84% /proj/sw_apps"

lets pretend the variable is called $df_stats

to get the pieces you want, you can either split the string into an array and then reference the array entry, or you can do a regex. both are reliable in this example, because your string cannot have any spaces in any other places. the space is your field delimiter.

my $df_stats="xhd-filer2:/vol/vol7/sw_apps 506854848 425468096 81386752 84% /proj/sw_apps";

my @fields=split(/ /,$df_stats);
print "available space=$fields[3], used=$fields[4]\n";

$df_stats=~/(.*?) (.*?) (.*?) (.*?) (.*?) (.*)/;
print "available space=$4, used=$5\n";

hope it helps

commented: Two ways for the price of one :) +2

Hi,
The code works.I got idea about RE.

But the problem is if we use df in perl we are getting

Filesystem           1K-blocks      Used Available Use% Mounted on
xhd-filer3:/vol/    1006437632  456241856 550195776  46% /home/abhinav

Like this i was getting into variable .so how can i get used and available into

variable.

Thanku

Try this modified version of one of dch26's solutions:

#!/usr/bin/perl
use strict;
use warnings;

my $df_stats = qx{df /home/}; #I don't have dir called /compare/ so I used /home/

my @fields=split(/\s+/,$df_stats); #Split on one or more whitespace characters

#Can we assume first 6 fields are column headers?
#If so, instead of the following
#print "available space=$fields[3], used=$fields[4]\n";

#Try this:
my $available = $fields[6+3];
my $used = $fields[6+4];

print "available space=$available, used=$used\n";

Running this on my computer (Linux platform) gives the following output:

available space=6655136, used=66830396

Thanks a lot.The code works good for me.

Thanku thanku

Hi,
i have another simple problem regarding mail format.

i was sending mail from my script about disk usage etc.

but i was unable to present the mail properly.while i was sending mail

the arrangement is disturbed.

the code is

my $p='%';
my $b='B';
my $gb='GB';
my $mailprog = '/usr/lib/sendmail';
open(MAIL,"|$mailprog -t");
print MAIL "To:abhinav\@xilinx.com\n";
print MAIL "From: disk script <no_reply\@xilinx.com>\n";
print MAIL "Subject:Team members Disk Usage \n\n";
print MAIL "TOTAL DISKSPACE IS $total1$gb\n\n";
print MAIL "USED DISKSPACE IS $used1$gb($usedp)\n\n";
print MAIL "AVAILABLE  DISKSPACE IS $avail($out$p)\n\n";

printf (MAIL "%7s","user");
printf (MAIL "%25s","path");
printf (MAIL "%22s","Usedspace");
printf (MAIL "%23s", "Used%\n");
print MAIL "______________________________________________________________________________\n";


print MAIL  "phaneesh\t $user1\t\t\t $space1$b\t\t$usedp1$p\n\n";
print MAIL  "chaitanya\t $user2\t\t\t $space2$b\t\t$usedp2$p\n\n";
print MAIL  "shefali\t $user3\t\t\t $space3$b\t\t\t$usedp3$p\n\n";
print MAIL  "sathiya\t $user4\t\t\t $space4$b\t\t\t$usedp4$p\n\n";
print MAIL  "shekhar\t $user5\t\t\t $space5$b\t\t$usedp5$p\n\n";
print MAIL  "pa_bash\t $user6\t\t\t $space7$b\t\t$usedp6$p\n\n";
print MAIL  "shared\t $user7\t\t\t $space7$b\t\t$usedp7$p\n\n";
#update with new user in future


close(MAIL);

my aim is to produce output like

TOTAL DISKSPACE IS 490GB

USED DISKSPACE IS 429GB(88%)

AVAILABLE  DISKSPACE IS 61244160(12.49%)

   user                     path             Usedspace                 Used%
______________________________________________________________________________
phaneesh	 /proj/sw_apps/phaneesh	        5.7GB		1.20%

chaitanya	 /proj/sw_apps/cdudha   	 144GB		30.61%

shefali	        /proj/sw_apps/smishra	 	 24GB		4.99%

sathiya	        /proj/sw_apps/sathiya		 84GB		17.81%

shekhar	        /proj/sw_apps/shekhar		 105GB		22.26%

pa_bash	        /proj/sw_apps/PA_BASH		 3.9GB		1.17%

shared	        /proj/sw_apps/shared		 3.9GB		0.82%

But i was unable .to present properly

Moreover output is not alike compared to my system and other systems

i.e if i send to my mail it is disturbed in someway when comapred to other mails in other systems

please try to guide or help me

you have shown us your desired output, but not your actual output. when i run your code i get something that looks quite like the above.....i cant replicate exactly because i have no data (source unix system)....but the idea is sound.
i did notice that you had $space7 on both last lines of PRINT MAIL, which is wrong.
i need more info if you'd like help. cheers

Hi,
Actually i want the mail to be same in all systems.

since i used as /t as as delimeter i was not getting.

i want to use printf but i dont know to use.

what actually i want is for example if i take

first line

phaneesh should come under user

/proj/sw_apps/phaneesh under path

5.7GB under used space

1.20% under used%

Exactly.but as i was using /t iwas unable as its width is differnt from system to system.

so in short i need help

in area that to print 4scalars under 4 headings exactly with good looking

in mail.

try to help.tanq

Hi,
Actually i want the mail to be same in all systems.

since i used as /t as as delimeter i was not getting.

i want to use printf but i dont know to use.

what actually i want is for example if i take

first line

phaneesh should come under user

/proj/sw_apps/phaneesh under path

5.7GB under used space

1.20% under used%

Exactly.but as i was using /t iwas unable as its width is differnt from system to system.

so in short i need help

in area that to print 4scalars under 4 headings exactly with good looking

in mail.

try to help.tanq

I haven't tried the sendmail program but I guess if you can get it to line up OK on the display that's a start. Let's try lining up the column headers with the first line of data.

#!/usr/bin/perl
use strict;
use warnings;

printf ("%7s","user");
printf ("%25s","path");
printf ("%22s","Usedspace");
printf ("%23s", "Used%\n");
print '_' x 79, "\n";

#From your desired output, I guess your variables have the following values
my $user1 = 'phaneesh';
my $path1 = '/proj/sw_apps/phaneesh';
my $space1 = '5.7';
my $b = 'GB';
my $usedp1 = '1.20';
my $p = '%';

printf ('%-17s%-20s%13s%2s%21s%s', $user1, $path1, $space1, $b, $usedp1, $p);

This gives the following output:

user                     path             Usedspace                 Used%
_______________________________________________________________________________
phaneesh         /proj/sw_apps/phaneesh          5.7GB                 1.20%

I find http://www.devdaily.com/blog/post/perl/reference-page-perl-printf-formatting-format-cheat-sheet a handy guide to printf.

Instead of having multiple lines with print MAIL to write the mail body, save the mail body to a variable and call that variable at sendmail pipe.

print MAIL "Subject: $sub\n\n";
print MAIL "$mailbody\n";

It doesn't make a good read when you say you want to use printf, but don't know how to do it. Why don't you just learn it?

And you need to use perl hashes to store disk usages for each user.

commented: Excellent advice. Your suggestion saves work in the long run. +2

actually i was a basic starting programmer.i will try the tasks you said and ask u for doubts
tanq

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.