The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Quick Question on sed command in shell script luft Shell Programming and Scripting 4 09-10-2008 05:36 PM
Guidance needed for quick script senthilkumar_ak UNIX for Advanced & Expert Users 4 08-08-2008 06:59 AM
quick script C shell ajp7701 Shell Programming and Scripting 1 03-18-2008 04:31 PM
Quick help needed in the Shell Script namishtiwari Shell Programming and Scripting 1 02-01-2008 09:44 AM
need a quick basic shell script help eb222 Shell Programming and Scripting 6 11-22-2007 09:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2008
pobman pobman is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 31
Quick script to display df as a CSV

Hi ,

Can some one show me how to display a df -k as a CSV.

I am guessing it is something like:

Code:
df -kP /home | awk ' {print $1 "," $2 "," $3 "," $4 "," $5 "," $6} '
but it does not output correctly as the fields are not in line.
I could try to use a delimiter but there is a space in the word "Mounted On"

I have also tried
Code:
for i in `df -kP` ; do echo $i |awk ' {print $1 "," $2 "," $3 "," $4 "," $5 "," $6 "," $7 } '; done
Again with bad results.

Is their an easy way to achieve this?

UPDATE:
I have the code almost working doing this:
Code:
         COUNTLINES=0
         NUM=`df -kP |wc -l`
         COUNTLINES=`expr $COUNTLINES + $NUM - 1`
         df -kP |head -n 1| awk ' {print $1 "," $2 "," $3 "," $4 "," $5 "," $6 " " $7} '
         df -kP |tail -n $COUNTLINES| awk ' {print $1 "," $2 "," $3 "," $4 "," $5 "," $6} '
         df -kP |tail -n $COUNTLINES| awk ' {print $1 "," $2 "," $3 "," $4 "," $5 "," $6} '

Last edited by pobman; 11-20-2008 at 09:38 PM..
  #2 (permalink)  
Old 11-20-2008
rk4k rk4k is offline
Registered User
  
 

Join Date: Nov 2008
Location: /planet-earth/indonesia/jakarta/
Posts: 11
If you want the output is Comma Separated Values like these :

Code:
/dev/sda2, 15519908, 4768020, 9963500, 33%, /
/dev/sda5, 45042712, 30363336, 12391320, 72%, /home
tmpfs, 253156, 36, 253120, 1%, /dev/shm
gvfs-fuse-daemon, 15519908, 4768020, 9963500, 33%, /home/user/.gvfs
/dev/sda1, 15566008, 12771972, 2794036, 83%, /media/disk
Then the script should be like :

Code:
 df -k | tr -s " " | sed 's/ /, /g'
Is that what you look for ?
  #3 (permalink)  
Old 11-20-2008
pobman pobman is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 31
Thanks rk4k

Pretty much perfect.

I am going to have to spend some time memorising sed.

One more thing though, is there an easy way to stop the line, Mounted On being split by a comma?
  #4 (permalink)  
Old 11-20-2008
rk4k rk4k is offline
Registered User
  
 

Join Date: Nov 2008
Location: /planet-earth/indonesia/jakarta/
Posts: 11
Talking

That would be :

Code:
df -k | tr -s " " | sed 's/ /, /g' | sed '1 s/, / /g'
I'm too still practicing how to "sed"ing properly.
  #5 (permalink)  
Old 11-20-2008
pobman pobman is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 31
Awesome, I'll be making a note of that for the future.

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0