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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use awk to compare values piped from df

Status
Not open for further replies.

osmosys8

IS-IT--Management
Oct 29, 2012
1
US
I want to use awk to compare values piped from "df" like so:

df:

Code:
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sda1      725649232 118249576 571080120  18% /
udev             2994044         4   [COLOR=#EF2929]2994040[/color]   1% /dev
tmpfs            1201132      1120   [COLOR=#EF2929]1200012[/color]   1% /run
none                5120         0      5120   0% /run/lock
none             3002824       352   3002472   1% /run/shm
/dev/sr0         5487686   5487686         0 100% /media/UDF Volume

Then pull out the red values:

2994040
1200012


And run a comparison:

if 2994040 > 1200012 then:
echo "udev exceeds tmpfs in size"
else echo "udev is smaller than tmpfs in size"
fi

Something like that. I've gotten this far:

Code:
#!/bin/sh
 awk '{print $c}' c=${1:-2}

I put the above into "colmn" and then ran:

df | ./column which outputs:

1K-blocks
725649232
2994044
1201132

5120
3002824
5487686

I don't know how to grab the values I want, and I don't want to use cut. I want it to be smart enough to read the lines that go with udev or tmpfs (or whatever lines I specify). I imagine that if I can grab the values then I can store them in variables somehow and then run some comparison logic, but I don't know how to put it all together.

Thanks!



 
Hi

Code:
df | awk '[navy]$1[/navy][teal]==[/teal][green][i]"udev"[/i][/green][teal]||[/teal][navy]$1[/navy][teal]==[/teal][green][i]"tmpfs"[/i][/green][teal]{[/teal]s[teal][[/teal][navy]$1[/navy][teal]]+=[/teal][navy]$4[/navy][teal]}[/teal]END[teal]{[/teal][COLOR=chocolate]printf[/color][green][i]"udev %s tmpfs in size"[/i][/green][teal],([/teal]s[teal][[/teal][green][i]"udev"[/i][/green][teal]]>[/teal]s[teal][[/teal][green][i]"tmpfs"[/i][/green][teal]])?[/teal][green][i]"exceeds"[/i][/green][teal]:[/teal][green][i]"is smaller than"[/i][/green][teal]}[/teal]'
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Note that on my machine there are 7 tmpfs entries in [tt]df[/tt]'s output, so I put my code to add them and compare the sums.

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top