LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grepping last word of output (https://www.linuxquestions.org/questions/programming-9/grepping-last-word-of-output-118605/)

rajatgarg 11-21-2003 03:10 PM

grepping last word of output
 
Hi all,

I am trying to write a script in perl.
It executes df -h and reads the input :
/dev/sda7 688M 45M 608M 7% /scratch
/dev/sda3 981M 17M 915M 2% /tmp
/dev/sda8 494M 282M 187M 61% /usr/vice/cache
/dev/sda5 981M 69M 862M 8% /var

I want to get input in an array ,
where it contains only last field namely -> /scratch, /tmp, /usr/vice/cache, /var

@arr = <xyz>;
takes the whole line as input and I can parse it. But when partition names vary from machines to machines, that ofcourse is not a good idea.

Kindly suggest what to do ???

Regards,
Rajat Garg
SU

david_ross 11-21-2003 03:37 PM

I beleive this should work for what you want:
Code:

#!/usr/bin/perl
@array=split(/\n/,`df`);
for ($done=1; $done<@array; $done++){
@fields=split(/\s/,$array[$done]);
$array[$done]=$fields[@fields-1];
print $array[$done]."\n";
}
exit;


inTrouble? 11-25-2003 08:34 AM

you can start with

df | cut -c57-


mfeat 11-25-2003 10:41 AM

df -h | awk '{ print $NF }'


All times are GMT -5. The time now is 06:56 AM.