For Programmers: Free Programming Magazines  


Home > Archive > AWK > September 2006 > newbie: don't understand NR==FNR{nums[$1];next}









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author newbie: don't understand NR==FNR{nums[$1];next}
Atropo

2006-09-11, 6:56 pm

I've seen several awk tutorial but none explain me that. tried to use
it 'coz is the usual recomentadion when using two files. what i
trying to do is this.
i have file1 wich column 1 i have to format to find it in others files
whose names start with date of today vg "OUT-20060911-##.out".
manually i first do a "cat file1.txt|awk 'OFS="-" {print
substr($1,1,3),substr($1,4,3)}' > file1_final.txt"
so in file1_final.txt i have a colunm that migth be on 1 or several
files..

How do i reading the column1 from file1 look for this column on others
files and get the info from those others files.?? can't figure out an
aproach.

have a var with the names of the files of today archivos=OUT-`date
+"%Y%m"`*.out, don't know if its useful..

Ted Davis

2006-09-11, 9:56 pm

On 11 Sep 2006 14:35:49 -0700, "Atropo" <lxvasquez@gmail.com> wrote:

>I've seen several awk tutorial but none explain me that. tried to use
>it 'coz is the usual recomentadion when using two files. what i
>trying to do is this.
>i have file1 wich column 1 i have to format to find it in others files
>whose names start with date of today vg "OUT-20060911-##.out".
>manually i first do a "cat file1.txt|awk 'OFS="-" {print
>substr($1,1,3),substr($1,4,3)}' > file1_final.txt"
>so in file1_final.txt i have a colunm that migth be on 1 or several
>files..
>
>How do i reading the column1 from file1 look for this column on others
>files and get the info from those others files.?? can't figure out an
>aproach.
>
>have a var with the names of the files of today archivos=OUT-`date
>+"%Y%m"`*.out, don't know if its useful..


The question was "Subject: newbie: don't understand
NR==FNR{nums[$1];next}"

Without knowing the context, it's difficult to say exactly what any
command does, but that one translates to

If the number of records read from this file is equal to the number
of records read in total (that is, if this is the first file) create
an array entry with the first field of this line as its index. Don't
do anything else. If not the first file, don't create the array
entry, but do whatever follows this line.


NR==FNR{nums[$1];next}
if( $1 in nums ){
your code for if the index from the first file exists in the
second goes here.
}

--
T.E.D. (tdavis@gearbox.maem.umr.edu) Remove "gearbox.maem" to get real address - that one is dead
Ed Morton

2006-09-12, 7:57 am

Atropo wrote:
> I've seen several awk tutorial but none explain me that. tried to use
> it 'coz is the usual recomentadion when using two files. what i
> trying to do is this.
> i have file1 wich column 1 i have to format to find it in others files
> whose names start with date of today vg "OUT-20060911-##.out".
> manually i first do a "cat file1.txt|awk 'OFS="-" {print
> substr($1,1,3),substr($1,4,3)}' > file1_final.txt"


UUOC.

Good: awk 'stuff' file
Bad: cat file | awk 'stuff'

> so in file1_final.txt i have a colunm that migth be on 1 or several
> files..
>
> How do i reading the column1 from file1 look for this column on others
> files and get the info from those others files.?? can't figure out an
> aproach.


In general, this will find the records from file2 who's first field
matches any first field from file1:

awk 'NR==FNR{arr[$1];next}$1 in arr' file1 file2

That approach has the problem that if file1 is empty, then undesriably
NR==FNR in file2. If you use gawk, you can test ARGIND instead, e.g.:

awk 'ARGIND==1{arr[$1];next}$1 in arr' file1 file2

if not, you can test FILENAME against the first argument:

awk 'FILENAME==ARGV[1]{arr[$1];next}$1 in arr' file1 file2

There's various other approaches but the above are the simplest.

> have a var with the names of the files of today archivos=OUT-`date
> +"%Y%m"`*.out, don't know if its useful..
>


You'd be better off just passing in the date part and constructing the
full string within awk. If you use gawk, you don't need that since it
has it's own data functions.

Ed
Ed Morton

2006-09-12, 7:57 am

Ted Davis wrote:

<snip>
> NR==FNR{nums[$1];next}
> if( $1 in nums ){


ITYM:

$1 in nums {

> your code for if the index from the first file exists in the
> second goes here.
> }
>

Regards,

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com