Unix Shell - Add columns

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Add columns





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Add columns
asiadbajobs@gmail.com

2007-05-24, 1:16 am

Hi,

I have a script output file that looks like
device date size free
- -- -------------- ----- ----- -----
RAW01 Nov122005 120g 102g
RAW02 NOV122005 120g 102g

I would like to just filter out the 3rd column(size) and then add the
values (or it can be more as well). So it should be
120 + 120 = 240. Based on how this value is divisible by 5, I want to
write more case statements.

Could you help me how to just strip the 3rd column and add the values
in a Korn shell script.

Ed Morton

2007-05-24, 1:16 am

asiadbajobs@gmail.com wrote:
> Hi,
>
> I have a script output file that looks like
> device date size free
> - -- -------------- ----- ----- -----
> RAW01 Nov122005 120g 102g
> RAW02 NOV122005 120g 102g
>
> I would like to just filter out the 3rd column(size) and then add the
> values (or it can be more as well). So it should be
> 120 + 120 = 240. Based on how this value is divisible by 5, I want to
> write more case statements.
>
> Could you help me how to just strip the 3rd column and add the values
> in a Korn shell script.
>


shell is an environment you use to call tools, it's rarely the best
choice for writing text processing scripts. In this case, it's trivial
in awk:

awk '{tot += $3}END{print (tot%5 ? "not " : "") "divisible by 5"}' file

Regards,

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com