I want to use optional variable passing in awk
suppose i have two file and i want to print the match/ unmatch values taking one or multiple column values as a identifier key.
file1
1 || 2 || 3
2 || 3 || 2
file 2
1 || 3 || 3
2 || 3 || 1
here is what i'm using you can find somewhat similar way to getting matched/unmatched values from here. awk unmatched with blank file
awk -v a="1:3" 'BEGIN{n=split(a,Z,":"); FS="[|]{2}";OFS="||"}{FILENAME==ARGV[1] for(i=1;i<=n;i++){a[$(Z[i])]++;next} !($(Z[i]) in a) }' file1 file2
as you can see rather than using multiple variable i'm replying on one variable
The reason i'm doing this because i want the identifier key number undefined, but there's something wrong in this one.
what am i doing wrong?
UPDATE: As you are not able to track my question here is more simplified version of the question.
let us say for file1 i want to print only column 1 so the command will be
print.awk
BEGIN{
FS="[|]{2}";
OFS="||"}
{
print $var
}
awk -v var=1 -f print.awk file1
this will print
1
2
But what if you want to don't want to change in print.awk
you want to use only single structure & pass in multiple variable
one solution is
print.awk
BEGIN{
n=split(a,Z,":"); FS="|"}{
for(i=1;i<=n;i++)
printf $(Z[i]);
print "";}
now if i want to print more than one columns i can simply call
awk -v a="1:3" -f print.awk file1
as you can see i have used split the ":" delimited values in a to put the values of variable a in an array n & then accessing the values to get the result.
Now to the question, suppose the scenario is 1. file 1 might be blank, so cannot use FNR==NR 2. there may be more than one columns to access, but the awk file cannot be changed as often, which means there will be one column number to be considered for sure, but there might be more than one to consider
so to test this logic i used that above awk code to find the matched values from file1 & file2.
awk -v m="1:3" 'BEGIN{n=split(m,Z,":"); FS="[|]{2}";OFS="||"}FILENAME==ARGV[1] {for(i=1;i<=n;i++){a[$(Z[i])]++;next} !($(Z[i]) in a) }' file1 file2
But as usual this code is not in working condition. so how can i achieve this?