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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to translate df -h output into a CSV format?
# 1  
Old 11-10-2016
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 ?

Code:
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%                -
----------------   --------   --------   ---------   ----   --------------


Last edited by Scrutinizer; 11-10-2016 at 04:40 PM.. Reason: code tags
# 2  
Old 11-11-2016
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.
# 3  
Old 11-11-2016
Try this - very simplistic, admittedly - approach:
Code:
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%,-
----------------,--------,--------,---------,----,--------------

# 4  
Old 11-11-2016
Quote:
Originally Posted by RudiC
Code:
sed 's/   */,/g' file

Cool! Sometimes, simple is best.

Maybe it would be a good idea to do add .... | grep -v ^- to get rid of the dashed lines.
# 5  
Old 11-11-2016
Code:
sed '/^---/d; s/   */,/g' file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-11-2016
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
# 7  
Old 11-11-2016
The output **is** CSV format. What are you missing?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

3. Shell Programming and Scripting

UNIX command output in csv format

I'm just wondering is there any way to capture the output of a unix command in a csv format. df -h gives the result of filesystem,free space,Used space, use %,mounted on. Is there a way to capture the command output and format it as comma sparated or fixed length file. (3 Replies)
Discussion started by: anita81
3 Replies

4. Shell Programming and Scripting

Csv format output file using scirpt

Hi All, I get the test result file daily after running the main test script. from the resultfile, need to fetch only server info and status and put them in tabular format in a file and as well in CSV format output file. I tried using awk command but am not able to put them in tabluar... (6 Replies)
Discussion started by: Optimus81
6 Replies

5. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

6. Shell Programming and Scripting

CSV format help

Hi team, In simple way i will explain.. i have a shell script which gives morethan one output echo"some code" ----> it gives one output echo"some code" ----> it gives one output echo"some code" ----> it gives one output echo"some code" ----> it gives one output i want all this to put... (0 Replies)
Discussion started by: darling
0 Replies

7. Shell Programming and Scripting

formatting into CSV format of SQL session output

I am getting a no of fields from a SQL session (e.g. select a,b,c from table). How do I convert the output values into CSV format . The output should be like this 'a','b','c', (4 Replies)
Discussion started by: mady135
4 Replies

8. Shell Programming and Scripting

Retaining the Unix CSV format in Excel format while exporting

Hi All, I have created a Unix Shell script whch creates a *.csv file and export it to Excel. The problem i am facing is that Users wants one of the AMOUNT field in comma separted values. Example : if the Amount has the value as 3000000 User wants to be in 3,000,000 format. This Amount format... (2 Replies)
Discussion started by: rawat_me01
2 Replies

9. Shell Programming and Scripting

format output in csv file

I am sending the output of a file to .csv file. The output should look like this: Total Customers Processed:,8 Total Customers Skipped:,0 Total Customers Added:,8 Total Customers Changed:,0 Total Policies Deleted:,0 Total Policies Failed:,0 total:,8 Now i want this output in... (1 Reply)
Discussion started by: Prashant Jain
1 Replies

10. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question