Can awk patterns be dynamically configured with variables?

Asked 2 years ago, Updated 2 years ago, 117 views

instead of simple variable expansion
 echo-e 1 2 3 \\n4 56 \\n789 |
awk $1 == 1, $2 == 2, $3 == 3"

If so, of course you can get line 1 2 3
How can I get patterns if I use variables here?

Specifically
Variables aa="\$1==1,\$2==2,\$3==3" Also include $n and equal sign in the variable.
awk'eval aaa' How can I get the same result as above by doing something like ←?

awk-va1=1,a2=2,a3=3'$1==a1,$2==a2,$3'

I would like to reiterate that it is not as simple as .

awk

2022-09-30 21:42

1 Answers

As the example of the person who is asking the question, I think it would be better to substitute the conditional expression for the shell variable cond and refer to it as $cond.

cond='$1==1&&$2==2&$3==3';printf'123\n456\n789\n'|awk "$cond"

[Results]

 1 2 3


2022-09-30 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.