【发布时间】:2018-10-11 00:28:12
【问题描述】:
我有一个名为 users.txt 的文件,其中包含以下格式的用户列表:
bob
john
alex
tom
我需要运行这个 AWK 语句并将这些名称中的每一个用作模式并输出到文件
awk '/PATTERN/{x=NR+6}(NR<=x){print}' input.txt >> output.txt
如何让 AWK 循环遍历每个名称并将它们用作搜索模式?
示例输入文件:
bob@servername
10/09/2018 19:11:19
50152 command issued.
weid: A1Pz64385236
job_name: xxx-xxx-xxx-xxx-xxx-xxx
command: fff-fff-fff-fff-
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
alex@servername
10/09/2018 16:33:55
50152 command issued.
weid: A1Pz64385236
job_name: xxx-xxx-xxx-xxx-xxx-xxx
command: fff-fff-fff-fff-
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
doug@servername
10/09/2018 13:22:66
50152 command issued.
weid: A1Pz64385236
job_name: xxx-xxx-xxx-xxx-xxx-xxx
command: fff-fff-fff-fff-
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::`
输出应该是这样的,只有 users.txt 文件中的用户(输入文件中有很多我不想看到的用户)
bob@servername
10/09/2018 19:11:19
50152 command issued.
weid: A1Pz64385236
job_name: xxx-xxx-xxx-xxx-xxx-xxx
command: fff-fff-fff-fff
【问题讨论】:
-
一定要用
awk吗?这看起来像是grep -f users.txt input.txt的工作。 -
或
grep -F -f users.txt -A 6 input.txt -
@Barmar 我需要找到每个模式,一旦找到该模式,就可以打印该模式和接下来的 6 行。据我所知,AWK 是最好的解决方案,除非你能告诉我其他方式。
-
这不是
-A 6选项到grep的作用吗?你真的让这件事变得比需要的更难。 -
@Barmar grep: 非法选项 -- F SunOS 5.10 Generic_150400-48 sun4u sparc SUNW,Sun-Fire-V490