模仿`grep的--only-matching`在AWK [英] mimicking `grep --only-matching` in awk

查看:185
本文介绍了模仿`grep的--only-matching`在AWK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了处理一堆输入,然后打印出prettily格式输出庆典脚本。目前,它是非常模块化 - 它产生万吨子shell和用途回声的grep 排序厕所,&安培; SED 很多,但我的工作有更大的 AWK 块替换多个块的功能,而提高效率

I've got a bash script that handles a bunch of input and then prints out prettily-formatted output. At the moment it's very modular -- it spawns tons of subshells and uses echo, grep, sort, wc, & sed a lot, but I'm working on replacing the functionality of multiple chunks with larger awk chunks, for better efficiency.

一挣扎:我一直在试图找出如何搜索输入特定字符串,只有打印我在寻找确切的事情。我一直在玩awk的匹配功能但没有成功呢。这里有一件事我想弄清楚如何融入一个更大的awk脚本的例子:

One struggle: I've been trying to figure out how to search input for specific strings, only printing the exact thing I'm searching for. I've been playing with awk's match function but haven't had any success yet. Here's an example of one thing I'm trying to figure out how to integrate into a larger awk script:

$ egrep -o "pae|lm|vmx|svm|ht" /proc/cpuinfo | sort -u
ht
lm
pae
vmx

如果我是用awk做同样的事情,我想每个包含我搜索的结果它发现字符串数组或变量结束。我看到它的主要问题是,每个字符串我在寻找可能存在多次输入。也许我只是需要买一个awk书...任何意见欢迎。

If I were to use awk to do the same thing, I'd want to end up with an array or variable containing each string I searched for that it found. The main problem as I see it is that each string I'm searching for might exist more than once in the input. Maybe I just need to buy an awk book... Any feedback welcome.

推荐答案

我认为这会做你的工作:

I think this will do your job:

的awk -v RS =PAE | LM | VMX | SVM | HT'!RT ={打印RT}'的/ proc内/ cpuinfo

或者如果你也需要做AWK排序:

Or if you also need to do sorting in awk:

gawk的-v RS =PAE | LM | VMX | SVM | HT'RT ={M [RT]} END {N = asorti(男,m_sorted)!;对于(i = 1; I< = N; ++ I){打印m_sorted [I]}}'的/ proc内/ cpuinfo

说明:我们设置记录分隔符 RS 必要的正则表达式和awk门店的精确匹配 RS 正则表达式中的 RT 变量。 RT 为空的最后一个记录,所以我们需要检查非空虚。

Explanation: we set record separator RS to necessary regex, and awk stores the exact match of the RS regex in the RT variable. RT is empty for the last record, so we need to check for non-emptiness.

分拣版本使用功能 asorti GAWK 扩展。

The sorting version uses function asorti which is gawk extension.

有关更通用的方法,看看匹配功能。例如,如果你可以设置这样的记录分隔所以比赛occures每个记录只有一次,那么解决方案并不复杂:
gawk的-v RS =your_separator匹配($ 0 / PAE | LM | VMX | SVM | HT,M){打印M [0]}

For more general approach, look into match function. For example, if you can set such record separator so match occures only one time per record, then the solution isn't complicated: gawk -v RS="your_separator" 'match($0, /pae|lm|vmx|svm|ht, m)" {print m[0]}

这篇关于模仿`grep的--only-matching`在AWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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