awk NR==FNR output control

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk NR==FNR output control
# 1  
Old 06-19-2011
awk NR==FNR output control

Hi Guys,

I have two files:





f1 has 48000 lines, and f2 has 68. I have been matching f1 $3 to f2 $1, and getting f3:


I would like f3 too look like this:


basically I want all of the fields for f2 to appear in the output as well.

Here's one of the things I've tried:

awk 'NR==FNR{a[NR]=$1;s=NR;next}{for(i=1;i<=s;i++) if(a[i]==$3){print a[i] "\t" $1,$2,$3}}' f1 f2 > f3

I've also tried matching f2 $1 to f1 $3 using the above. The problem is that, while I get all of the f2 fields, my awk command does not preserve the row order of f1, and I could not come up with a way to do that, so at the moment controlling the output of a f1 to f2 comparison seems to be the easiest approach.

Thanks for your help, I am certainly grateful.
# 2  
Old 06-19-2011
The short answer is to process f2 first, then process f1. This will reduce your memory footprint as you'll only save 68 things in a[] rather than 48K things.

The long answer is to be a bit more clever which might also help speed things up. Your programme will loop through the entire contents of file f1 for each record in f2 (48,000 * 68) testing to see if there's a match. Instead, use the hash capabilities of awk to your advantage.

This example assumes that the 'key' (field 1 in file 2) can occur multiple times and so we must do a bit of looping for each f1 record, but the only looping needed when reading limited to the number of duplicate 'keys' that existed in f2 for the current f1 record. If f2 will not have duplicates, then the code can be simplified more, but not knowing you exact data, this general case will work for either. We also don't need to make an explicit check to see if the key in the current record matches the one saved from f2.

awk -v f2=f2 '
    BEGIN {
        while( (getline<f2) > 0 )   # read and collect records from f2
            key = $1;
            ki = kidx[key]++;        # track number of duplicate keys (0 based)
            k2rec[key,ki] = $0;      # save unique record by key and dup count
        close( f2 );

        key = $3;
        for( i = 0; i < kidx[key]; i++ )          # for each duplicate of key
            printf( "%s\t%s\n", k2rec[key,i], $0 );   # print f2 record, followed by current f1 record
' <f1 >f3

Hope this makes sense.

Last edited by agama; 06-19-2011 at 12:07 PM.. Reason: Corrected printf to output f2 then f1
This User Gave Thanks to agama For This Post:
# 3  
Old 06-19-2011
Thanks agama, your approach makes perfect sense. I appreciate your time and your efforts on my behalf.


---------- Post updated at 11:58 AM ---------- Previous update was at 11:11 AM ----------

Worked perfectly, thanks again for your time.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Assigning a variable to be the value of FNR at a certain line

Sorry for the probably strangely worded title but I don't really know how else to put it. Background context: Post processing LAMMPS simulation data. tl;dr: I'm making two spheres collide, every defined timestep the simulation outputs a bunch of data including total energy of the particles,... (10 Replies)
Discussion started by: ThomasP
10 Replies

2. Shell Programming and Scripting

Explanation of FNR in this awk script

To merge mutiple *.tab files as: rs1 A A rs2 A A rs3 C C rs4 C Cfile2.ind rs1 T T rs2 T T rs3 G G rs4 G Gand rs1 B B rs2 B B rs3 L L rs4 L LOutput: AA TT BB AA TT BB CC GG LL CC GG ... (4 Replies)
Discussion started by: yifangt
4 Replies

3. Shell Programming and Scripting

awk --> selective printout with FNR

Hi everybody! need some awk-support. i want a line-selective printout of a file. wat i normally will do with ... awk ' FNR==8' sample.txt But now i need the data from line 8, 10 and the following data from line13 to 250 wich is not end of the file. I tried allready to combine it but without... (2 Replies)
Discussion started by: IMPe
2 Replies

4. Shell Programming and Scripting

Tip: alternative for NR==FNR in awk

Example: $ cat file1 2 3$ cat file2 1 2 3 4 5 6The following awk script works like a charm, NR==FNR is true for file1, the remainder runs for file2: awk ' NR==FNR {A; next} ($1 in A) ' file1 file2 2 3Now have an empty file1: >file1and run the awk script again. The result is empty... (8 Replies)
Discussion started by: MadeInGermany
8 Replies

5. Shell Programming and Scripting

How to control a null output in EMC storage?

I dont want to print the output in a EMC VMAX storage if it says "The specified device was not found", however it is not letting me do it. I am trying to run this command: symaccess -sid xxxx list -type storage -devs 1234 output: The specified device was not found I just want the script... (1 Reply)
Discussion started by: prodigy06
1 Replies

6. Shell Programming and Scripting

Awk FNR==NR question

awk -F'' 'FNR==NR {a=$2; next} {$1=a} 1' $useralias ${entries} >> ${entries}_2 Hi, Is there anyway to alter this command so that if it does not find a match it will just leave the line alone instead of replacing what it doesn't find with a blank space? (4 Replies)
Discussion started by: Jazmania
4 Replies

7. UNIX for Dummies Questions & Answers

Multiple Column print after lookup using NR==FNR (awk)

foo.txt FAMID IID AFF SEX Group AgeCat Dis1 Dis2 Dis3 Dis4 Dis5 Dis6 Dis6 AMD0001 Mayo_49542 1 2 AMD 8 1 1 1 1 1 1 1 AMD0002 Mayo_49606 1 1 AMD 3 1 1 1 1 ... (7 Replies)
Discussion started by: genehunter
7 Replies

8. Shell Programming and Scripting

error "awk: (FILENAME=- FNR=23) fatal: division by zero attempted"

Hi , I have file : after i run this command : there are error can we print blank line if output error ?? thanks.. ^^ (4 Replies)
Discussion started by: justbow
4 Replies

9. Shell Programming and Scripting

awk NR==FNR compare 2 files produce a 3rd

hi, i have two files, both with 3 columns, the 3rd column has common values between the two files and i want to produce a 3rd file with 4 columns. file 1 a, ,b c file 2 a, b ,d I want to compare the 3rd value and if a match print to file 3 with the 3 columns from the first file... (11 Replies)
Discussion started by: borderblaster
11 Replies

10. Shell Programming and Scripting

Awk: different between NR and FNR

As I know: FNR: The ordinal number of the current record in the current file. NR: The ordinal number of the current record from the start of input. I don't understand really differency between NR and FNR. Who can explain it for me? And give me an example. Thanks (1 Reply)
Discussion started by: anhtt
1 Replies
Login or Register to Ask a Question