-1

I need to pass some variable in the following awk cmd,

awk -F, 'NR==FNR{a[\$0]++;next}!(a[\$6])' "file1" "file2" > output

Instead of providing the value 6, I need to pass it as a variable?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Siva
  • 9
  • 3

1 Answers1

0

Here's an example using -v to pass a variable to awk. Also note that if you use single quotes around the awk program, you don't want to escape the $s.

$ cat file1.txt
1
4

$ cat file2.txt
1,2
3,4

$ awk -F, -v i=1 'NR==FNR{a[$0]++;next} !(a[$i])' file1.txt file2.txt
3,4

$ awk -F, -v i=2 'NR==FNR{a[$0]++;next} !(a[$i])' file1.txt file2.txt
1,2
jas
  • 10,715
  • 2
  • 30
  • 41