【发布时间】:2019-01-01 15:20:22
【问题描述】:
我的 Linux shell 脚本的此问题来自this other。
案例场景:
$ cat test.txt
C1 C2 C3
1 a snow
2 b snowman
snow c sowman
搜索第三个字段包含“雪”的行可以正常工作:
$ awk '$3 ~/snow/' test.txt
1 a snow
2 b snowman
但我需要通过使用变量来做到这一点:
$ word="snow"
$ echo $word
snow
$ awk -v variable="snow" '$3 ~/variable/' test.txt
$ awk -v variable="$word" '$3 ~/variable/' test.txt
$
可以看出,没有结果。
如何执行基于变量的 AWK 搜索?
【问题讨论】: