Re: array values not visible outside while loop? scope issue?
- From: Luqman <luqman_ngs@xxxxxxx>
- Date: Fri, 09 Mar 2007 14:47:49 +0100
Am Fri, 09 Mar 2007 03:19:12 +0100 schrieb Janis Papanagnou:
awk 'NR==FNR{a[$1];next}{print $3 in a}' received.txt sent.txt
That seems to work. Can you please explain your script a bit so that I can
customize it to my needs in future and also learn something.
NR counts the number of records read so far (incrementing over all files)
FNR counts the number of records read so far just for the current file
NR==FNR is only true while reading the first file
a[$1] adds value $1 into set a (an associative array a used like a set)
next proceeds with the next input record (skipping any subsequent action)
If the condition NR==FNR is not true any more, then we're in the second file
and the first action {a[$1];next} is not processed,
The second action {print $3 in a} is not guarded by any condition so always
executed for each line (of the second file).
The test ($3 in a) will return a boolean value (as integer 0 or 1), 1 if the
value $3 is in the set a, 0 otherwise; that numeric value is printed
NR==FNR: Does it read the whole first file "received.txt" before
proceeding?
The program will read the first file (received.txt) and then the second file
(sent.txt) in the order they are given as arguments to awk; it will always
read the complete files. NR==FNR is just a boolean predicate to determine
whether we are in the first or in any subsequent file.
awk is really great for processing files. Thanks to you and others on the
list with helpful replies I have now a working script.
--
Luqman
.
- References:
- array values not visible outside while loop? scope issue?
- From: Luqman
- Re: array values not visible outside while loop? scope issue?
- From: Vassilis
- Re: array values not visible outside while loop? scope issue?
- From: Luqman
- Re: array values not visible outside while loop? scope issue?
- From: Janis Papanagnou
- Re: array values not visible outside while loop? scope issue?
- From: Luqman
- Re: array values not visible outside while loop? scope issue?
- From: Janis Papanagnou
- array values not visible outside while loop? scope issue?
- Prev by Date: Re: array not accessible outside a loop? variable scope issue?
- Next by Date: Re: array not accessible outside a loop? variable scope issue?
- Previous by thread: Re: array values not visible outside while loop? scope issue?
- Next by thread: Re: array values not visible outside while loop? scope issue?
- Index(es):
Relevant Pages
|