How to translate df -h output into a CSV format?

Hi,

Can someone please let me know as how I can send the below df -h format of a linux system into a CSV format ?

Resource           Size GiB   Used GiB   Avail GiB   Use%   Cleanable GiB*
----------------   --------   --------   ---------   ----   --------------
/data: pre-comp           -   288912.1           -      -                -
/data: post-comp    87397.6    63816.6     23581.0    73%                -
----------------   --------   --------   ---------   ----   --------------

I would proceed as follows:

(1) First read the header line and find the character positions of the fields. Since you know the "column titles", this is easy to do.

(2) Then read the following lines:

(2a) A line starting with a dash can be ignored.

(2b) In any other line, since you know (from step (1)) the length of each field, you can split the line into the pieces to get the field content. You will probably want to trim trailing spaces from the fields.

Now to the question, which language to use to implement this. When creating CSV, I suggest in generally to use a language, which has built-in support for writing CSV files, because you don't have to worry about proper quoting/escaping, if the field content contains characters which are significant to the CSV format. For example, Ruby or Perl would be good languages. In your special case, I don't believe that the df output will contain such characters, so you could also use awk. It is technically possible to do it with shell scripting in zsh and (perhaps with more effort) in bash, but I wouldn't find it a bit inconvenient.

Try this - very simplistic, admittedly - approach:

sed 's/   */,/g' file
Resource,Size GiB,Used GiB,Avail GiB,Use%,Cleanable GiB*
----------------,--------,--------,---------,----,--------------
/data: pre-comp,-,288912.1,-,-,-
/data: post-comp,87397.6,63816.6,23581.0,73%,-
----------------,--------,--------,---------,----,--------------

Cool! Sometimes, simple is best.

Maybe it would be a good idea to do add .... | grep -v ^- to get rid of the dashed lines.

sed '/^---/d; s/   */,/g' file
1 Like

Thanks for your reply but I want to finally get the format into a CSV file in rows and colums

---------- Post updated at 10:03 AM ---------- Previous update was at 10:03 AM ----------

Thanks for your reply but I want to finally get the format into a CSV file in rows and columns

---------- Post updated at 10:03 AM ---------- Previous update was at 10:03 AM ----------

Thanks for your reply but I want to finally get the format into a CSV file in rows and columns

The output **is** CSV format. What are you missing?

so the output I am getting it as below

~/scripts]$ sed '/^---/d; s/   */,/g' dd-usage-out.txt
Resource,Size GiB,Used GiB,Avail GiB,Use%,Cleanable GiB*
/data: pre-comp,-,288912.1,-,-,-
/data: post-comp,87397.6,63816.6,23581.0,73%,-

I want to convert this more into presentable format like a excel where I can seperate out the columns and rows

I don't quite get it. First you said that you want to have CSV format, and this **is** CSV format. Now you want to convert CSV format into Excel format? This is a different problem, and I suggest that you open a new thread for this.

Having said this, why do you want to create xls / xlsx? After all, Excel can read CSV.

... which is exactly what you asked for: .csv . What DID you expect?
For presenting it in EXCEL you don't need any conversion at all, just read it in as a text file. If need be, capture the procedure in a small macro.

Thanks for the clarification. I think I got what I was looking for.