Hacker News new | past | comments | ask | show | jobs | submit login

I've tried doing that with awk a few times, and find I just haven't written enough to make it non-painful. I did write a ton of Perl at one point, and it seems to have hijacked the associated neurons.

Spending a weekend messing about with awk until I'm comfortable is one line in a very, very long list of things I'll probably never get to.




Any programming language with a line- and field-based orientation should do it. Awk is easy because of the implied input loop over the data, and the ability to slice by columns.

If you want additional outputs, you simply create them. For a trivial example, if you've got a list of revenues and expenses (say, by month), and want a report with monthly and annual totals (I'll assume only one year is fed, though the example could be expanded:

    #!/usr/bin/gawk -f
    
    BEGIN { printf( "%-8s %-8s | %-8s\n", "revenue", "costs", "profit" )}
    
    {
        rev = $1; cost = $2; profit = rev - cost;
        totrev+=rev; totcost+=cost;
        printf( "%8.2f %8.2f | %8.2f\n", rev, cost, profit )
    }
    
    END { 
        printf( "----------------------------\n")
        printf( "%8.2f %8.2f | %8.2f\n", totrev, totcost, totrev - totcost ) 
        printf( "============================\n")
    }
That's the equivalent of a spreadsheet with columns for revenues, costs, and profits, with a summary line. But you can run it over any annual datasource.

Now let me check if that's right.... OK, not too far off, though yes, I'd goofed bits (the first time).




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: