ksh to get a csv one line file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charlyrosario
    New Member
    • Jun 2007
    • 1

    ksh to get a csv one line file

    I am using ksh to get one line csv file from the output of df -k. The only information i need is: File System Name and FS Usage Example:

    /,50,/usr,45,/var,43 All info in ONE line

    I am using this script:
    #!/bin/ksh
    cat dftmp | grep "%" | awk '{printf "%d,%s\n", $5, $6}' > dftmp2
    cat dftmp2 | gawk '{var=var "," $0} END{print substr(var,2)}'

    Now problem is the following:
    in dftmp2 i have:
    /,50
    /usr,45
    /var,43

    But the ouput of the second command does not give me everything in one line. All info appeared overlapped in one line as:
    ,43,/var

    Any idea why?. I am using GNU linux 2.6.18-8.el5
  • radoulov
    New Member
    • Jun 2007
    • 34

    #2
    Code:
    df -kP|{ read;while read f b u a c m;do printf "%s,%s," "$f" "${c%\%}";done;}
    ... and if you don't want the last "," and you want a trailing new line:

    Code:
    df -kP|awk 'NR==2{v=$1","$5;next}{v=v","$1","$5}END{print v}' FS="[ %]*"

    Comment

    Working...