awk with two files

I have two files, 1 column each and both are n lines long, eg

-12
-11.836734693877551
-11.673469387755102
...

and

02
04
06
...

I'd like to pattern match line i in file 1, determine its line number and see what the value of the corresponding line in file 2 is.

I can think of various ways of doing it, join etc, but I'd like to use awk for a more elegant solution and one that will help me advance with awk.

I cerrently have (ksh):

file2val=$(awk -v var=$file1val '$1 ~ var {print NR}' file1 file2)

Which gets me the file1 line number, but I'm not clear on how to do the rest.

Thanks in advance,

Jon

Something like that ?

awk 'NR==FNR{a[NR]=$1;next}{$0= a[FNR] FS $1}1' file2 file1

That pastes one file alongside the other. I'd like to pluck a single value from file 2 based on the line number of a pattern match from file1.

nawk 'BEGIN{

  while ((getline x < "a" ) > 0) {
     print x;

     getline y < "b"

    if ( x == "bat" ){
      print y;
      }
    }
  }' /dev/null

infiles:

==> a <==
ant
bat
cat
dog
emu

==> b <==
ant aardvark
bat basilisk
cat chameleon
dog dugong
emu elephant

output:

ant
bat
bat basilisk
cat
dog
emu

Badly formatted and cryptic since you wanted to advance with awk :slight_smile:

NR==FNR&&f1<1&&$1=="a"{f1=NR}NR>FNR&&f1==FNR

Performance can be improved by adding nextfile (in gawk) or exit.

Wow, now thats what I call terse. I'll have a play, thanks.