'converting the output of df -h into an array and then modified it's output

I want to converting the output of df -h into an array and then modified it's output with bash script.

For example the output of command is:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda4        28G  480M   26G   2% /var
/dev/sda2        28G   45M   26G   1% /tmp
/dev/sda5       275G  4.6G  256G   2% /home
tmpfs           790M   84K  789M   1% /run/user/1000

The output should be:

Filesystem      Size  Used Avail Use% 
device4        28G  480M   26G   2% 
device2        28G   45M   26G   1% 
device5        275G  4.6G  256G   2% 
Tmp           790M   84K  789M   1% 

I know I should set IFS=$'\n' to have this output in an array but I have no idea how can I recognize device name and replace with the proper name.

Thank you for helping me to solve the problem.



Solution 1:[1]

This works for me:

#!/bin/bash

df -h | sed 's#\(.*\) \(.*\)$#\1#' | sed 's#Mounted##' | sed 's#/dev/sda\(.\)#device\1  #'
  • the first sed removes the last column
  • the second sed removes the word "Mounted". The last column header is "Mounted on", so the first sed only removes "on".
  • the last sed replaces /dev/sda by device.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nic3500