Re: File parsing





On 11/29/2007 3:35 PM, vibgyorbits wrote:
10 abc
20 abcc
30 abcd
40 xycz
42 wxy
5 abc
20 abcd
30 abcc

So, What i intend to do is:
Build an array or associatiave array...
such as

During the first pass:
search for the "FIRST" occurances of column 2.
So it would be:
"abc" - 10
"abcc" - 20
"abcd" - 30
& so on...

During the second pass:
search for the "SECOND" occurance of column2 & modify the earlier
array as:
"abc" 10 - 5 = "abc" 5
abcc 20 - 30 = "abcc" -10

I know this is doable in just one pass,but i'm stuck at:

1. I read the ffirst occurance of "abc" (column 2) - abc 10
2. I can NOT read until the next occurance ot "abc"- abc 5
3. I can NOT come back a LINE after where i was at step 1.

Is there a better / quicker way to do this?

PS # this is not a homework question, nor this is a puzzle / interview
qn. :)


Just check for the array[$2] already holding a value and, if not, populate it
with the current $1, otheriws subtract the current $1 from the current array[$2]
value:

awk '{a[$2]=(a[$2]=="" ? $1 : a[$2] - $1)} END{for (i in a) print i,a[i] }' file

Regards,

Ed.

.



Relevant Pages

  • File parsing
    ... Build an array or associatiave array... ...
    (comp.lang.awk)
  • Re: File parsing
    ... Build an array or associatiave array... ... I can NOT read until the next occurance ot "abc"- abc 5 ...
    (comp.lang.awk)
  • Re: Need help with user-written routine in SOR$
    ... > ADRS2 is the other record contents by reference ...
    (comp.os.vms)
  • Re: Unvollendetes read(STDIN_FILENO)
    ... Wenn ABC eine zusammenhängende Sequenz ist, ... einzelnes readgarantiert alles holt, also ABCD, und nicht ... mit einem generalisierten Terminal-Interface auskommt, ... fuer Unsinn Du Dir ausdenken kannst, um Deinen Unwillen, es richtig zu ...
    (de.comp.os.unix.programming)
  • Re: Index / match combination
    ... Try this, array entered: ... Microsoft Excel MVP ... "ABC" in Column A to find. ... again an array formula. ...
    (microsoft.public.excel.misc)