Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Posts
    5

    Unanswered: 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

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    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

  3. #3
    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!

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Is field 2 in file one in ascending order?

  5. #5
    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.

  6. #6
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •