If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > passing FNR from file to awk, multiple files.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-11, 21:37
binarybyzantine binarybyzantine is offline
Registered User
 
Join Date: Jan 2011
Posts: 5
passing FNR from file to awk, multiple files.

Hi everyone! Long time reader, first time poster.

I have two files, FILE 1 contains two columns. Column 2 are #'s that are NR's for FILE 2.

I.e.

FILE 1;

183088561 4
37922422 10

And FILE 2 is just a bunch of data, i.e.

FILE 2;

75076818 6577.50 353 20 51 16
75086808 6558.50 353 20 51 26
75096799 6550.06 353 20 51 36
75106799 6549.20 353 20 51 46
75116789 6553.60 353 20 51 56
75126780 6561.52 353 20 52 06
75136780 6571.62 353 20 52 16
75146770 6582.85 353 20 52 26
75156761 6594.52 353 20 52 36
75166751 6606.00 353 20 52 46
75176751 6616.95 353 20 52 56


I want to print the lines in FILE 2 associated with the NR from column 2 in FILE 1.

I.e. the script should print;

75106799 6549.20 353 20 51 46
75166751 6606.00 353 20 52 46

These are simple files, the real one would be a few dozen lines for FILE 1 and 10k+ lines for FILE 2.

I was trying to do with with a 1-line awk statement..(since its part of a much larger script...)

awk 'NR==FNR {a[$1]=$2; next}; END NR==a{$1] {print $0}' file1 file2 > tmp

which doesnt work...

any suggestion?

-Thanks!

ashton
Reply With Quote
  #2 (permalink)  
Old 01-15-11, 15:50
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
Code:
#!/bin/ksh
while read a b
do
   while read f1 f2 f3 f4 f5 f6
   do
     if [ $b = $f6 ]  #modify this line for your exact comparison
       then
       echo f1 f2 f3 f4 f5 f6 >>output
     fi
   done <file2
done <file1
Reply With Quote
  #3 (permalink)  
Old 01-15-11, 16:09
binarybyzantine binarybyzantine is offline
Registered User
 
Join Date: Jan 2011
Posts: 5
Smile Hmmm....

Thanks for the reply, but I'm trying to stay away from nested loops. I dont want to have to run through the file M*N times, to intensive.

Also for anyone else, I'm not comparing b to f6. I'm comparing b to the line number of file2.

I'm really trying to bang this out in an awk 1-liner.

Thanks though!
Reply With Quote
  #4 (permalink)  
Old 01-15-11, 16:32
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
Is field 2 in file one in ascending order?
Reply With Quote
  #5 (permalink)  
Old 01-15-11, 17:23
binarybyzantine binarybyzantine is offline
Registered User
 
Join Date: Jan 2011
Posts: 5
Hmmm.....

Yes, if I remember correctly (it was late last night in my office) I sorted the output for file1 in ascending order based on field 2.
Reply With Quote
  #6 (permalink)  
Old 01-24-11, 15:11
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Talking G-Awwwwk

Try this:
Code:
gawk '
  BEGIN {while ((getline line < "FILE1.txt") > 0){split(line,x); n[++i]=x[2]}}
  {for (i in n) if (NR == n[i]) print $0;}
' FILE2.txt
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On