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!

Help needed to format the output of df command 1

Status
Not open for further replies.

powerfulperl

Programmer
Feb 20, 2010
10
IN
Hi, I want to properly format the output of the df command and store the output in the array, the problem here is, in the third line if u guyz c when the location file system path extends the size, %, mounted on.. goes to next line when i try to push the values in the array line by line i want this to appear in one line.. any help is much appreciated.

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 497861 224899 247258 48% /
/dev/sda1 147764 77278 62857 56% /boot
/dev/mapper/rootvg-tmp
6063688 105684 5649988 2% /tmp
 
Here is the code which i hv written
#! /usr/local/bin
open(DF, "df|");
while(<FH>){
chomp();
next if (/^\/proc\b/);
printf("FH %s\n",$_);
push(@df_output, $);
}
close (DF);
When i push the line in the array, it should complete data in one line.
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 497861 224899 247258 48% /
/dev/sda1 147764 77278 62857 56% /boot
/dev/mapper/rootvg-tmp
6063688 105684 5649988 2% /tmp
 
Like Annihilannic keeps telling you, use the -P option. And while we are at it, lets stop messing about reading the piped output of the command as a file, shall we?
Perl:
use strict;
use warnings;

my @df_output = qx{df -P};

print @df_output;
If you really need to get rid of the newlines at the end of the entries you can
Perl:
chomp @df_output;
to do them all at once.

Many Unix commands have an option to suppress header lines in the output to make them easier to use when scripting, but unfortunately df doesn't appear to be one of them... [sad]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
#! /usr/local/bin
open(DF, "df -Ph|");
while(<FH>){
chomp();
next if (/^\/proc\b/);
printf("FH %s\n",$_);
push(@df_output, $);
}
close (DF);

i hv tried the option df -Ph and it works for linux but it gives an error for solaris OS. So now what i wanted to do is,i want to keep the df command, i want to format that data after pushing the values in the array.
I want to rearrange it in this way. (Data on 4th line is on one line which i want to do)

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 497861 224899 247258 48% /
/dev/sda1 147764 77278 62857 56% /boot
/dev/mapper/rootvg-tmp 6063688 105684 5649988 2% /tmp
 
I hear you... I had similar problems writing portable scripts that parse df output. HP-UX is even more annoying because df output is cryptic and you need to use bdf to get anything remotely like other Unixes. You'll find that -h is not widely supported either.

Try this:

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@df_output[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]DF, [red]"[/red][purple]df|[/purple][red]"[/red][red])[/red][red];[/red]
[olive][b]while[/b][/olive][red]([/red]<DF>[red])[/red][red]{[/red]
        [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][red])[/red][red];[/red]
        [olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^[purple][b]\/[/b][/purple]proc[purple][b]\b[/b][/purple][/purple][red]/[/red][red])[/red][red];[/red]
        [black][b]my[/b][/black] [blue]@a[/blue]=[url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red];[/red]
        [gray][i]# not enough fields?  append the next input line[/i][/gray]
        [olive][b]if[/b][/olive] [red]([/red][blue]@a[/blue]<[fuchsia]6[/fuchsia][red])[/red] [red]{[/red] [blue]$_[/blue] .= <DF> [red]}[/red]
        [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url][red]([/red][blue]@df_output[/blue], [blue]$_[/blue][red])[/red][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] [red]([/red]DF[red])[/red][red];[/red]

Annihilannic.
 
I forgot to put another chomp() in there to remove the new line from the appended line... or you could just move the chomp() down to just before the push().

Annihilannic.
 
I have tried the above and it worked fine.. i want to use the same code in the below example as well... what changes do i need to make apart from the above which u suggested..
I have added tmp_array2 on the same lines as mentioned above. will that work and is that enough, objective is same.. thanx


socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or last USE_SOCKETS;

CHILD->autoflush(1);
PARENT->autoflush(1);

if ($pid = fork) {
close PARENT;
$'df_ok=1;
local $SIG{ALRM} = sub {$'df_ok=0; kill ('KILL',$pid); print "Killed the $df child";};
alarm 180;
while (<CHILD>) {
chomp();
next if (/^\/proc\b/);
@tmp_array2=split;
if(@tmp_array2<6){$_ .=<CHILD> }
chomp();
push(@'df_output, $_);
}
close CHILD;
alarm 0;
waitpid($pid,0);
}
 
This time also i want the output of df command in one line. In the above code the lines

@tmp_array2=split;
if(@tmp_array2<6){$_ .=<CHILD> }

were not there i added it .. i just want to confirm.. whether doing this way is right or not...
 
socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)

What is the CHILD here?? is that a file handle??

and also thanks for replying.. :)
 
It is a socket, see perldoc -f socketpair. Presumably the rest of the code that you haven't shown me involves running the df process from a child process and passing the output to the parent through a socket.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top