Thanks to dteske for the idea

df -h | while read line; do [ -n “$var” ] || echo $line; var=1; echo $line | grep p8; done

NOTE: how you can do several things with each line (like you can with awk), using a while read VARIABLE loop (format while read line; do STUFF; done;). (VARIABLE here for us is line)

Output:

Filesystem Size Used Avail Capacity Mounted on
p8/image2 2.2G 691M 1.6G 30% /
p8/mnt 1.0G 48M 976M 5% /mnt
p8/core 1.0G 1.4M 1.0G 0% /core
p8/master_config 1.6G 44K 1.6G 0% /opt/pixel8/master_config

Normal Output:

# df -h
Filesystem          Size    Used   Avail Capacity  Mounted on
p8/image2           2.2G    691M    1.6G    30%    /
devfs               1.0K    1.0K      0B   100%    /dev
p8/mnt              1.0G     48M    976M     5%    /mnt
/dev/md0             62M    140K     57M     0%    /tmp
tmpfs                33G    756K     33G     0%    /cache
p8/core             1.0G    1.4M    1.0G     0%    /core
p8/master_config    1.6G     44K    1.6G     0%    /opt/pixel8/master_config
/dev/fuse0            0B      0B      0B   100%    /proc
/dev/fuse1            0B      0B      0B   100%    /proc/sys
zroot               9.9T    1.6G    9.9T     0%    /cloudfs
zroot/kvk-CC-01     9.9T    797M    9.9T     0%    /cloudfs/kvk-CC-01
zroot/kvk-CC-02     9.9T    797M    9.9T     0%    /cloudfs/kvk-CC-02
zroot/kvk-CC-03     9.9T     41K    9.9T     0%    /cloudfs/kvk-CC-03
devfs               1.0K    1.0K      0B   100%    /usr/home/jail/dev

So what happens in that command?

Red text is your typical grep statement df -h | grep p8 everything in between is what prints the first (header) line & whichever line greps “p8”

Normal output:

# df -h | grep p8
p8/image2           2.2G    691M    1.6G    30%    /
p8/mnt              1.0G     48M    976M     5%    /mnt
p8/core             1.0G    1.4M    1.0G     0%    /core
p8/master_config    1.6G     44K    1.6G     0%    /opt/pixel8/master_config

NOTE: its missing the header, so its hard to tell which column is what (if your not familiar with the columns)

 

Here is what happens. df -h is piped to a while read line loop. Basically every line of the df -h output will be set to the variable $line. We determine the variable line in the read statement (we could of used other word, like i). The first time the loop evaluates, the line variable will be set to the first line of the output of df -h (the header).┬áThe way I got it to only print the first line is by checking for a set variable $var. The first time its run, var wont be set (unless something is using the $var variable in your system; in which case you can change the var to something else or surround the entire command with parenthesis and run it again so that it runs in a subshell and doesnt interfere), so that [ -n “$var” ] will evaluate as false and that echo $line will print that line (which is the first line). Immediately after that we set the variable to some trivial value (var=1, I could of set it to var=”grampa” if I wanted to). After that we echo the line thru our grep statement which will only print it, if grep finds the contents.

Leave a Reply

Your email address will not be published. Required fields are marked *