在 shell 脚本中使用 gawk
我想做一些类似的事情:
for i in 1 2 3
do
gawk '{if ($i==$2) {print $0;}}' filename
done
这可能吗?
谢谢
I want to do something of the sort:
for i in 1 2 3
do
gawk '{if ($i==$2) {print $0;}}' filename
done
is this possible?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了避免丑陋的引用,您可以使用 gawk 的变量传递功能:
In order to avoid ugly quoting, you can use gawk's variable passing feature:
假设您想要 gawk 脚本,
您可以执行以下操作:
Assuming that you want the gawk script to be
you can do:
我想到了对脚本的两种可能的解释:
filename
中的第二个字段以下是循环的两个版本:
Two possible interpretations of your script come to mind:
filename
Here are both versions of the loop:
当然,这是可能的,但它可能不会做你想做的事。
...想一想,您想要它做什么?从你写的内容来看还不是很清楚。您可能需要更改的一件事是用双引号替换单引号,因为变量(如
$i
)不会替换为单引号字符串。Well sure, it's possible, but it probably won't do what you want it to do.
...come to think of it, what do you want it to do? It's not really clear from what you've written. One thing you'll probably have to change is to replace the single quotes with double quotes because variables (like
$i
) aren't substituted into single-quoted strings.