Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: compare val...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 15 Topic 2141 of 2345
Post > Topic >>

Re: compare values in the same field in consecutive rows--and store

by "z.entropic" <subPlanck@[EMAIL PROTECTED] > Jan 24, 2008 at 02:35 PM

On Jan 24, 3:33=A0pm, Ed Morton <mor...@[EMAIL PROTECTED]
> wrote:
> On 1/24/2008 2:25 PM, z.entropic wrote:
>
>
>
>
>
> > On Jan 24, 2:41 pm, Ed Morton <mor...@[EMAIL PROTECTED]
> wrote:
>
> >>On 1/24/2008 1:19 PM, z.entropic wrote:
> >><snip>
>
> >>> Here is a fragment of my input file:
>
> >>>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> >>>100 =A0 =A0 =A0 =A024479.33 =A0 =A0 =A0 =A014399.09 =A0 =A0 =A0
=A01/23=
/2008 19:55 6 =A0 =A0 =A0 1 =A0 =A0 =A0 0 =A0 =A0 =A0 0 =A0 =A0 =A0 3.293
=
=A0 1.287
> >>>101 =A0 =A0 =A0 =A024480.25 =A0 =A0 =A0 =A014400.01 =A0 =A0 =A0
=A01/23=
/2008 19:55 6 =A0 =A0 =A0 1 =A0 =A0 =A0 0 =A0 =A0 =A0 0 =A0 =A0 =A0 3.296
=
=A0 1.288
> >>>102 =A0 =A0 =A0 =A024480.36 =A0 =A0 =A0 =A00.11 =A0 =A01/23/2008
19:55 =
7 =A0 =A0 =A0 1 =A0 =A0 =A0 0 =A0 =A0 =A0 -0.00185954 =A0 =A0 3.167 =A0
1.28=
7
> >>>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> >>>Thus, if field $6 in line 100 is equal to 6 AND field $6 in line 101
> >>>is equal to 7, store the value 14399.09 in array1[1] and 1.287 in
> >>>array2[1]. When the next match is found, store the two values in
> >>>array1[2] and array2[2], etc. =A0Basically, I'm comparing the same
> >>>fields in consecutive rows.
>
> >>Try this:
>
> >>awk '($6=3D=3D7)&&(p6=3D=3D6){array1[++n]=3Dp3;array2[n]=3Dp11}
{p=3D$6;=
p3=3D$3;p11=3D$11}' file
>
> >>You should use an array for the "p" (previous) field values if you to
ne=
ed to
> >>access more of them.
>
> >> =A0 =A0 =A0 =A0Ed.
>
> > I think my example is a bit confusing due to poor formatting (copying
> > from Excel with the wrong date format didn't help...) =A0In essence, I
> > can't get the script working even after some changes etc., so let me
> > explain again as best as I can. =A0Here is an interesting section frm
> > one of my data files, this time with proper formatting that awk would
> > see (tab-separated fields):
>
> > 100 24479.32 14399.08 1/23/2008 7:55:39 PM 6 1 0 =A00 =A0 =A0 =A0 =A0
=
=A03.293399
> > 101 24480.25 14400.01 1/23/2008 7:55:40 PM 6 1 0 =A00 =A0 =A0 =A0 =A0
=
=A03.293234
> > 102 24480.36 =A0 =A0 0.10 1/23/2008 7:55:41 PM 7 1 0 -0.00185954
3.16682=
6
> > 103 24480.46 =A0 =A0 0.21 1/23/2008 7:55:41 PM 7 1 0 -0.00185932
3.03483=
6
>
> > Simply put, I want to find pairs of lines in which the counter in
> > field $7 changes, here from 6 to 7, and then store in array array1[1]
> > the value found in field $11 (3.293234, line 101). The next pair of
> > found lines would change the array counter to 2 (array[2]).
>
> So now we're back to one array? ok, look:
>
> $ awk '($7=3D=3D7)&&(p7=3D=3D6){array[++n]=3Dp11} {p7=3D$7;p11=3D$11}
END{=
for (i in array)
> print i, array[i]}' file
> 1 3.293234
>
> > Once I figure out with your help how to do that, I'll try to expand
> > this script to store more values, including some from line 102 in the
> > example above.
>
> If the above still isn't what you're looking for either, maybe posting a
l=
ittle
> more sample input and some expected output would help.
>
> =A0 =A0 =A0 =A0 Ed.- Hide quoted text -
>
> - Show quoted text -

Your script works--in part, probably because I underspecified the
requirements.  I think the problem is a bit more complex; I'll provide
a larger  example of input and output.

I believe this kind of a problem may be of interest to a wider group
of readers and awk users as it concerns data extraction and processing
that I, at least, often encounter.

z.e.
 




 15 Posts in Topic:
compare values in the same field in consecutive rows--and store
"z.entropic" &l  2008-01-24 10:53:49 
Re: compare values in the same field in consecutive rows--and st
Ed Morton <morton@[EMA  2008-01-24 12:58:25 
Re: compare values in the same field in consecutive rows--and st
"z.entropic" &l  2008-01-24 11:19:28 
Re: compare values in the same field in consecutive rows--and st
Ed Morton <morton@[EMA  2008-01-24 13:41:45 
Re: compare values in the same field in consecutive rows--and st
Ed Morton <morton@[EMA  2008-01-24 13:52:12 
Re: compare values in the same field in consecutive rows--and st
"z.entropic" &l  2008-01-24 12:25:04 
Re: compare values in the same field in consecutive rows--and st
Ed Morton <morton@[EMA  2008-01-24 14:33:01 
Re: compare values in the same field in consecutive rows--and st
"z.entropic" &l  2008-01-24 14:35:52 
Re: compare values in the same field in consecutive rows--and st
Ed Morton <morton@[EMA  2008-01-24 16:40:22 
Re: compare values in the same field in consecutive rows--and st
"z.entropic" &l  2008-01-24 18:56:50 
Re: compare values in the same field in consecutive rows--and st
Ed Morton <morton@[EMA  2008-01-28 08:56:07 
Re: compare values in the same field in consecutive rows--and st
"z.entropic" &l  2008-01-28 07:46:59 
Re: compare values in the same field in consecutive rows--and st
"z.entropic" &l  2008-01-28 09:21:25 
Re: compare values in the same field in consecutive rows--and st
Ed Morton <morton@[EMA  2008-01-28 11:30:39 
Re: compare values in the same field in consecutive rows--and st
"z.entropic" &l  2008-01-29 05:34:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Sep 4 23:54:57 CDT 2008.