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?
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?
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