1832620 Members
2532 Online
110043 Solutions
New Discussion

Re: extract data

 
SOLVED
Go to solution
Cheng Wee
Advisor

extract data

How can i extract field number 5 without the percentage sign n assign it to a variable.

for example,

/dev/vg00/lvol5 614400 177916 411617 30% /home

for this case i hope to get 30 instead of 30%


Rgs

Cheng Wee
8 REPLIES 8
Mark Grant
Honored Contributor
Solution

Re: extract data

expr "/dev/vg00/lvol5 614400 177916 411617 30% /home" : '.* \(.*\)%'
Never preceed any demonstration with anything more predictive than "watch this"
James Murtagh
Honored Contributor

Re: extract data

Hi Cheng Wee,

Probably not the best way....but it works :

# size=$(# bdf | awk '/vg00\/lvol5/ {print $5}' | cut -d% -f1)

You would change the search pattern enclosed in the /.../ for a more general search.

Cheers,

James.
James Murtagh
Honored Contributor

Re: extract data

D'oh! Obviously take the "#" out before the bdf....my cut and paste antics again.
T G Manikandan
Honored Contributor

Re: extract data

#bdf|awk '{print $5}'|awk -F "%" '{print $1}'
Heiner E. Lennackers
Respected Contributor

Re: extract data

They may be many ways, e.g.:

echo "/dev/vg00/lvol5 614400 177916 411617 30% /home" | awk -F "[\t %]+" '{print $5}'

or

echo "/dev/vg00/lvol5 614400 177916 411617 30% /home" | sed -e 's/^[^%]*[\t ]\([0-9]*\)%[^%]*$/\1/'

and many more :)
if this makes any sense to you, you have a BIG problem
Cheng Wee
Advisor

Re: extract data

Hi Guys,

Thks for the solution, i exam all n all are working as expected.

MarkSyder
Honored Contributor

Re: extract data

bdf|grep /dev/vg00/lvol5|awk '{print $5}'|sed 's/%//g'

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Mark Grant
Honored Contributor

Re: extract data

Actually, if youw ant a version that allows for NFS mounted filesystems and filesystems that go over two lines and will work ignore swap and /proc (on solaris) you could try this old thing I just dragged up of my hard disk

#!/usr/bin/perl
@FILESYSTEMS=(`bdf 2>/dev/null`);
while($filesystem=pop @FILESYSTEMS){
chop($filesystem);
next if $filesystem =~ /.*Filesystem.*/;
next if $filesystem =~ /swap.*/;
next if $filesystem =~ /proc.*/;

($waste,$waste2,$waste3,$waste4,$percentage,$fs)=split " ", $filesystem;

if($fs eq ""){ # A two line df filesystem output
$fs=$percentage;
$percentage=$waste4;
$filesystem=pop @FILESYSTEMS;
}

next if $filesystem =~ /:/; # Lose NFS filesystems
$percentage =~ /(.+)%/; # drop the % sign
print "$fs $percentage\n";
}
Never preceed any demonstration with anything more predictive than "watch this"