1752762 Members
4880 Online
108789 Solutions
New Discussion юеВ

awk scripting help!

 
Susan Harris
Occasional Advisor

awk scripting help!

Help ! I'm on a time limit and need to figure something out. I have a file, 3 columns, that I need to do some addition/subtraction on. Here is a sample:

1 1415767 532483
1 1978079 573705
2 233409 1714
2 259988 1865

What needs to happen, is for every row that has the same $1 (all the 1's, 2's,etc.), I need to take the $2 of the first row for that $1, and then either add or subtract the $2 of the next row down, based on which number is larger. After that is done for all the same $1's, then move to next $1 (ie. move from 1 to 2) and do the same for those. I'm really stumped with this one...
Thanks,
Sue
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: awk scripting help!

This is really rather easy but I am going to do you a favor by not giving you the solution. You simply need to think in terms of associative arrays where $1 becomes the subscript of a set of pararllel arrays.

You also need to better define the problem: 1) What happens on the last member of a set of $1's? 2) After you add of subract using the secondf line, does this 'second' line become the new 'first' line or do you skip to the next line and start over?

Is the data already sorted on the first column? If not, can it be piped thru sort -n before awk?

I think when you answer these questions, your answer will pop out.

If it ain't broke, I can fix that.
Thomas J. Harrold
Trusted Contributor

Re: awk scripting help!

Show us the desired output based on your 4-line sample. This will help me understand what (exactly) you are trying to do.

-tjh
I learn something new everyday. (usually because I break something new everyday)
harry d brown jr
Honored Contributor