2

trying to match fields 1,3 to fields 1,2 in another file and print line of second file. First file is tab delimited and second is csv delimited. Unexpected token error?
file1

1 x 12345 x x x

file2

1,12345,x,x,x

script

awk -F',' FNR==NR{a[$1]=$1,$3; next} ($1,$2 in a) {print}' file1 file2 > output.txt
sjsam
  • 21,411
  • 5
  • 55
  • 102
mxttgen31
  • 123
  • 1
  • 7

2 Answers2

3

same idea, but doesn't depend on uniqueness of the first field but the pair instead

$ awk 'NR==FNR{a[$1,$3]; next} ($1,$2) in a' file1 FS=, file2

1,12345,x,x,x
karakfa
  • 66,216
  • 7
  • 41
  • 56
1

You almost nailed it !

awk 'NR==FNR{first[$1]=$3;next} $1 in first{if(first[$1]==$2){print}}' file1 FS="," file2

Output

1,12345,x,x,x

Notes

  1. Since the field separator is different for both the files, we have changed it in between files.
  2. This script makes an assumption that the first field of each file is unique, else, the script breaks

See [ switching field separator ] in between files.

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102