如何在awk脚本的搜索模式中使用变量 [英] how to using variables in search pattern in awk script

查看:64
本文介绍了如何在awk脚本的搜索模式中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在输入匹配模式时找到匹配的进程时打印pid:

I want to print the pid when finding matched process while the match pattern is inputted:

ps aux | awk -v in="$1" '/in/{print $1}'

以前的awk句子似乎不正确.在Google中检查了许多结果后,例如,我在以下更改我的脚本,但仍然无法工作:

It seems the former awk sentence is not right. After checking many results in google like this, I change my script in the following but still cannot work:

ps aux | awk -v in="$1" '/$0 ~ in/{print $1}'

ps aux | awk -v in="$1" '($0 ~ in) {print $1}'

推荐答案

您的所有尝试都非常接近.问题是 in awk 中的保留关键字.

You are fairly close in all your attempts. Problem is that in is a reserved keyword in awk.

您可以使用:

ps aux | awk -v var="$1" '$0 ~ var {print $1}'

或者使用非正则表达式:

Or else non-regex way:

ps aux | awk -v var="$1" 'index($0, var) {print $1}'

这篇关于如何在awk脚本的搜索模式中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆