Re: array values not visible outside while loop? scope issue?



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

.



Relevant Pages

  • Re: still a new user -- help with the paste command? still a new user -- help with the (UNIX) past
    ... Ralph Reinbeck wrote: ... > I'd like to write a script, though, that does this for the first file ... > in each directory, then the second file in each directory, then the ...
    (comp.unix.shell)
  • Re: Trying to make awk work for me...
    ... record from the first file passed to it first since that record would now appear to be only in the first file and the loop handles the first file first, then it'll print the record from the second file passed to it second since that record would now appear to be only in the second file and the loop handles the second file second. ... What I was trying to say was that rather than displaying those records that appear to have changed in the order they should have been read (top to bottom of file), they were displayed in reverse. ... awk, there's a couple of options, for other awks you just need to create an index of the kesy rather that using "for ". ...
    (comp.lang.awk)
  • Re: data algebra with two files
    ... first file 'calcA.dat' has ... second file 'calcB.dat' has ... for example data*0.3 substitute data in calcB.dat file: ... how to write the script for this? ...
    (comp.unix.shell)
  • Re: Interesting data storage problem
    ... > write out to files a fair amount of data, on the rough order of 10 MB. ... number is the block's location in the second file. ... specifically recorded in the first file. ... the record data along with the extra data position and size. ...
    (comp.lang.java.programmer)
  • Re: [OT] Accepting files (question)
    ... The way I operate the program under Windows XP is to make a shortcut icon ... it detect and use that second file. ... A shell script can solve the problem. ... The first time it creates a temporary driver script to later call your ...
    (comp.os.msdos.djgpp)