what am i missing? :( 
Author Message
 what am i missing? :(

Hello all,

I'm driving myself nuts :(  This should be easy, but i just havent learned
enough to finish such a simple task.

I'm working with two files: file1 has data.  File2 is a single-field list
of words.  All i am trying to do is check if the word in field6 of file1
appears anywhere in file2.  If it appears in file2, then print the line
with 0 0 appended on the end.  If it doesnt appear in file2, then print
the whole line with 1 1 appended on the end.

I am using "awk '{ print $0 }' file1" to send each line, one at a time, to
another awk file doing the comparison:

awk '{ print $0 }' file1 | awk -f append.digits.awk

Here is append.digits.awk:

BEGIN { i = 1 }
# try to insert values from file2 into an array
{ while ( getline < "file2" > 0 ) {
        list[i] = $0
        i++ }
# try to see if $6 appears in the list array
{ if ( $6 in list )
        print $0,0,0
    else
        print $0,1,1

Quote:
}

What happens is that if $6 appears in file2, then the final line
in file2 is printed instead with the 1 1 appended; i was hoping that the
whole line from file1 is printed with 0 0 appended :(   If $6 doesnt
appear in file2, then the whole line from file1 is printed with 1 1
appended.  That's the behavior i was hoping for with a no-match.

All help is appreciated; i've been spending days on this... i never get
enough uninterrupted time to read through my sed & awk book in any kind of
coherent fashion, so i end up hacking and hacking away at this simple
simple thing wihtout success :(



Sun, 12 Jan 2003 03:00:00 GMT  
 what am i missing? :(


Quote:

>Hello all,

>I'm driving myself nuts :(  This should be easy, but i just havent learned
>enough to finish such a simple task.

>I'm working with two files: file1 has data.  File2 is a single-field list
>of words.  All i am trying to do is check if the word in field6 of file1
>appears anywhere in file2.  If it appears in file2, then print the line
>with 0 0 appended on the end.  If it doesnt appear in file2, then print
>the whole line with 1 1 appended on the end.

Here's how I'd do it:

awk 'FILENAME="file2"{a[$0]=1; next}
     a[$6]==1 {print $0, "0 0"; next}
     {print $0, "1 1"}' file2 file1

The first line loads the array from file2;
the next says stop processing the record and get the next record
and start processing it from the top.  This causes subsequent
programming to be skipped.

The second line prints if field 6 is in the loaded array

The third line prints any record that hasn't been dealt with,
that got this far, i.e. wasn't in file2 and for which field 6
wasn't in the loaded array.

I didn't bother looking at what you'd done and correcting it, as
writing the above was much easier for me.

Chuck Demas
Needham, Mass.

Quote:

>I am using "awk '{ print $0 }' file1" to send each line, one at a time, to
>another awk file doing the comparison:

>awk '{ print $0 }' file1 | awk -f append.digits.awk

>Here is append.digits.awk:

>BEGIN { i = 1 }
># try to insert values from file2 into an array
>{ while ( getline < "file2" > 0 ) {
>    list[i] = $0
>    i++ }
># try to see if $6 appears in the list array
>{ if ( $6 in list )
>    print $0,0,0
>    else
>    print $0,1,1
>}

>What happens is that if $6 appears in file2, then the final line
>in file2 is printed instead with the 1 1 appended; i was hoping that the
>whole line from file1 is printed with 0 0 appended :(   If $6 doesnt
>appear in file2, then the whole line from file1 is printed with 1 1
>appended.  That's the behavior i was hoping for with a no-match.

>All help is appreciated; i've been spending days on this... i never get
>enough uninterrupted time to read through my sed & awk book in any kind of
>coherent fashion, so i end up hacking and hacking away at this simple
>simple thing wihtout success :(

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 12 Jan 2003 03:00:00 GMT  
 what am i missing? :(
Thanks so much for your reply!

Someone mentioned that my problem was thinking that the "in" statement looked at the
contents of an array; i guess it only looks at the index value.

Ahh, i see how you're doing it.  Awk must automatically initialize values to 0 then?
I'm not quite sure i understand how the file2 file1 at the end works, but i'll see if
i can figure that out.

Thanks again!
Coventry, RI

Quote:

> Here's how I'd do it:

> awk 'FILENAME="file2"{a[$0]=1; next}
>      a[$6]==1 {print $0, "0 0"; next}
>      {print $0, "1 1"}' file2 file1

> The first line loads the array from file2;
> the next says stop processing the record and get the next record
> and start processing it from the top.  This causes subsequent
> programming to be skipped.

> The second line prints if field 6 is in the loaded array

> The third line prints any record that hasn't been dealt with,
> that got this far, i.e. wasn't in file2 and for which field 6
> wasn't in the loaded array.

> I didn't bother looking at what you'd done and correcting it, as
> writing the above was much easier for me.

> Chuck Demas
> Needham, Mass.



Mon, 13 Jan 2003 03:00:00 GMT  
 what am i missing? :(

% I'm working with two files: file1 has data.  File2 is a single-field list
% of words.  All i am trying to do is check if the word in field6 of file1
% appears anywhere in file2.  If it appears in file2, then print the line

% I am using "awk '{ print $0 }' file1" to send each line, one at a time, to

It would be faster to use cat.... Anyway, there's no need for this. You can
put
 awk -f append.digits.awk file1

and awk will read lines one by one from file1.

% Here is append.digits.awk:
%
% BEGIN { i = 1 }
% # try to insert values from file2 into an array
% { while ( getline < "file2" > 0 ) {

One problem is that this getline overwrites $0. You ought to do this
in a BEGIN section, anyway:
 BEGIN {
   for (i = 1; getline < "file2" > 0; i++)
      list[i] = $0
   close("file2")
 }

% # try to see if $6 appears in the list array
% { if ( $6 in list )

Now, the indices of list[] are 1, 2, ..., so this will print whenever
$6 is a number less than or equal to the number of lines in file2. What
you really want is this:

 BEGIN {
   while (getline < "file2" > 0)
      list[$0] = ""
   close("file2")
 }

 { if ($6 in list)
%       print $0,0,0
%     else
%       print $0,1,1
% }

--

Patrick TJ McPhee
East York  Canada



Mon, 13 Jan 2003 03:00:00 GMT  
 what am i missing? :(


Quote:
>Thanks so much for your reply!

>Someone mentioned that my problem was thinking that the "in"
>statement looked at the contents of an array; i guess it only looks
>at the index value.  
>Ahh, i see how you're doing it.  Awk must automatically initialize values
>to 0 then?

Yup, all unassigned variables are 0 or "" until you assign them a
value.  No need to initialize variables or arrays.

Quote:
>I'm not quite sure i understand how the file2 file1 at the end
>works, but i'll see if i can figure that out.

Merely input.  Awk can take many files as input, not just one, and awk
processes them sequentially, in this case, file2 first and then file1.

Quote:

>Thanks again!
>Coventry, RI

You're welcome, neighbor!

Chuck Demas
Needham, Mass.

Quote:


>> Here's how I'd do it:

>> awk 'FILENAME="file2"{a[$0]=1; next}
>>      a[$6]==1 {print $0, "0 0"; next}
>>      {print $0, "1 1"}' file2 file1

>> The first line loads the array from file2;
>> the next says stop processing the record and get the next record
>> and start processing it from the top.  This causes subsequent
>> programming to be skipped.

>> The second line prints if field 6 is in the loaded array

>> The third line prints any record that hasn't been dealt with,
>> that got this far, i.e. wasn't in file2 and for which field 6
>> wasn't in the loaded array.

>> I didn't bother looking at what you'd done and correcting it, as
>> writing the above was much easier for me.

>> Chuck Demas
>> Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 13 Jan 2003 03:00:00 GMT  
 what am i missing? :(

Quote:

> Here's how I'd do it:

> awk 'FILENAME="file2"{a[$0]=1; next}
>      a[$6]==1 {print $0, "0 0"; next}
>      {print $0, "1 1"}' file2 file1

I'm an awk newbie myself, but shouldn't the pattern
in the first line read  FILENAME=="file2"  ?  I would
think that unless you did that, both file 1 AND file 2
would be used for initialization.


Mon, 13 Jan 2003 03:00:00 GMT  
 what am i missing? :(

Quote:
> You can put  awk -f append.digits.awk file1
> and awk will read lines one by one from file1.

Ahh, ok; i wasnt exactly sure what would happen calling that way.

Quote:
> One problem is that this getline overwrites $0. You ought to do this
> in a BEGIN section, anyway:

You're right.  In a later attempt, i realized it would be best in the BEGIN section.
Thanks for reinforcing that.  I also forgot to call close("file2").

It's overwriting $0 in the array?  Hmmm, is the i++ ever getting incremented at all
in the while loop here.  Here's what i had written again:
{
 while ( getline < "file2" > 0 ) {
        list[i] = $0
        i++ }
 }

I understand how you are doing yours:

Quote:
>  BEGIN {
>    for (i = 1; getline < "file2" > 0; i++)
>       list[i] = $0
>    close("file2")
>  }
> Now, the indices of list[] are 1, 2, ..., so this will print whenever
> $6 is a number less than or equal to the number of lines in file2.

Thanks for explaining what was happening in the way i wrote it: much appreciated!


Tue, 14 Jan 2003 03:00:00 GMT  
 what am i missing? :(


Quote:


>> Here's how I'd do it:

>> awk 'FILENAME="file2"{a[$0]=1; next}
>>      a[$6]==1 {print $0, "0 0"; next}
>>      {print $0, "1 1"}' file2 file1

>I'm an awk newbie myself, but shouldn't the pattern
>in the first line read  FILENAME=="file2"  ?  I would
>think that unless you did that, both file 1 AND file 2
>would be used for initialization.

Yup, a typo on my part.  It should be:

awk 'FILENAME=="file2"{a[$0]=1; next}
     a[$6]==1 {print $0, "0 0"; next}
     {print $0, "1 1"}' file2 file1

Now if I could just stop making them.  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 14 Jan 2003 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Am i missing something with CW5Pe?

2. Am I missing something here?

3. Am I missing some patches on C4 OLE ?

4. Am I missing something here?

5. What am I missing????

6. What runtime file am I missing?

7. Displaying and Image in a simple report - What am I missing

8. Browse Column Counts - What am I missing?

9. What am I missing?

10. What am I missing

11. CW2.003 - Multiple Menu Bars - what am I missing

12. Clarion 4 Scroll Bars - Am I Missing Something

 

 
Powered by phpBB® Forum Software