-3

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?

Community
  • 1
  • 1
bongboy
  • 147
  • 1
  • 15
  • so what is the desired output? This piece of code seems to be coming from one of your previous questions, mind to link it? – fedorqui Jun 03 '15 at 13:11
  • @fedorqui no this question is not from previous question, the example is just to get the unmatched values between the two files. & i just thought rather than inserting for each and every variable to define column identity its better to use some other generic method. – bongboy Jun 03 '15 at 13:20
  • Edit your question to explain what you mean by "unmatched" (regular expression or string or numeric or some other kind of comparison?) and by `the identifier key number` and show the expected output given that input. btw you have `{FILENAME==ARGV[1]` instead of `FILENAME==ARGV[1]{` - put the condition OUTSIDE of the action block, not inside it. – Ed Morton Jun 03 '15 at 14:20

1 Answers1

0

The next command causes the next line to be read and the line processing sequence to be restarted. So your for loop will never get beyond its first iteration, which will be executed for every line.

rici
  • 234,347
  • 28
  • 237
  • 341