Manipulate df output with sed

1.1k views Asked by At

I have a question about sed.

Output:

Filesystem              avail
rpool/ROOT/s10_u11_201704 244719726

Wanted information:

s10_u11_201704

I tried:

df -b / | sed '1d;s/.*\/\(*\ \)\ .*/\1/g'

The \(*\ \) does not work.

2

There are 2 answers

4
P.... On BEST ANSWER

using awk :

df -b / |awk -F'/' 'NR>1{split($NF,a," ");print a[1]}' 
s10_u11_201704

Using sed:

df -b / |sed -r '1d;s|(^.*)/([^ ]+).*|\2|g'
s10_u11_201704

Disclaimer: df -b is not available in any of available distros to me.

0
RomanPerekhrest On

Short awk approach:

df --output=source | awk -F'/' '{print $NF}'

  • --output=source (--output[=FIELD_LIST]) - use the output format defined by FIELD_LIST

  • -F'/' - treating / as field separator

  • $NF - the last field value