shellawkzimbra

Mail accounts and loose folders report to CSV using AWK. How to achieve that from this example?


I'm using this shellscript to generate a list of inboxes and the sizes of all folders, for each user, as follows:

john.doe@mydomain.com's max mailbox size = 0 MB, current mailbox size = 18,78 GB.

size (MB)  msgcount     unread folder
--------- --------- ---------- ----------------------------
        0         0          0 /Chats
       42         0            /Drafts
    13118     28014         37 /Inbox
        0         6          0 /Junk
        0         1          0 /Orders
      323     13385         17 /Raster
     5772      3760          0 /Sent
        1       183          0 /Payments
        0         2          0 /Trash
-------------------------------------------------------

I need to mine data from this and throw it into CSV where on each line I'll have the email account and the values for Trash, Sent and Junk folders values. The problem is the "Inbox" because, as you can see, users created folders outside the tree (like "Raster" and "Payments"). So I need to find a way to sum everything that's not "thrash/sent/junk" for each user from this report.


Solution

  • The following awk can be used as a starting point. It collect data, and read the data into memory, and print the summary at the END event.

    awk -v OFS=, '
    function do_print () {
            print user, s_trash, n_trash, s_sent, n_sent, s_junk, n_junk, s_other, n_other
    }
    
    /max mailbox/ { user = $1 ;
            s_trash = n_trash = s_sent = n_sent = s_junk = n_junk = s_other = n_other = 0
            next ;
    }
        # Parse lines starting with '/'
    $4 ~ /^\/Trash/ { s_trash += $1 ; n_trash += $2 ; next }
    $4 ~ /^\/Sent/ { s_sent += $1 ; n_sent += $2 ; next }
    $4 ~ /^\/Junk/ { s_junk+= $1 ; n_junk += $2 ; next }
       # Everything else goes to other
    $4 ~ /^\// { s_other += $1 ; n_other = $2 ; next }
    
      # Print whenever there is a line of '='
    /==================/ { do_print() }
    
    END { do_print() }
    '