-1

I think I've looked everywhere and I'm not having much luck. I'm trying to create a simple automated billing report. I need to create a CSV with 3 columns. like this

Folder   Size   Billing Code
Folder1  300M   XXXXX
Folder2  600M   XXXXX
Folder3  200M   XXXXX

I can get a du -sh and use awk to arrange it how I want and I get most of what I'm attempting to do. however I don't know how to create a header before each column. I've looked for methods in bash, perl, and python and having limited success. If I could just figure out how to make the column header labels I think i'd be fine from there.

Lain
  • 2,166
  • 4
  • 23
  • 47
Rob C
  • 11

3 Answers3

4

Try the csv module in Python. Specifically, you'll want the writeheader() method, it's pretty straightforward.

Alternately, if you're already doing this in a shell script, why not just echo the header line to the file first, then making sure that the subsequent commands use the append operator ( >> )?

Kyle Maxwell
  • 617
  • 6
  • 20
3

What about adding the names after you render the values?

$ sed 1i"This is the first line" - < /proc/loadavg
This is the first line
3.14 3.48 3.58 2/533 17661
jary
  • 1,161
  • 6
  • 9
1

An approach with perl:

#!/usr/bin/perl

use strict;
use warnings;
use File::Find;

open my $fh, '>:encoding(utf8)','your outfile.csv';

# write the first line with the column names
print $fh "Folder,Size,Billing Code\n";

# Do your folder calculations
my $dirpath = "/your/path";
my $bytes;
find(\&folders,$dirpath);
close $fh;

sub folders {
    if (-d $_) {
        $bytes = 0;
        find(\&size,$_);
        $bytes = $bytes/1_048_576; # Convert Bytes to Mega Bytes
        print $fh $_.",".$bytes."M,your_billing_code \n";
    }
}

sub size {
    if(-f $_){
        $bytes += -s _; # This is not a typo
                        # -X _ does a filesystem operation on the 
                        # last used stat structure
                        # this saves new filesystem calls and therefore performance
                        # see http://perldoc.perl.org/functions/-X.html for more info
    }
}

This writes the CSV File without external commands like du and works on every platform. Also check Perl's CSV Modules for more options

Demnogonis
  • 3,172
  • 5
  • 31
  • 45