本文介绍了如何在awk中获取regExp的子表达式值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在分析日志,其中包含以下信息:

I was analyzing logs contains information like the following:

y1e","email":"","money":"100","coi

我想获取金钱的价值,我用'awk'像这样:

I want to fetch the value of money, i used 'awk' like :

grep pay action.log | awk '/"money":"([0-9]+)"/' ,

那我怎么能得到([0-9] +)中的子表达式值?

then how can i get the sub-expression value in ([0-9]+) ?

推荐答案

如果您有GNU AWK(gawk):

If you have GNU AWK (gawk):

awk '/pay/ {match($0, /"money":"([0-9]+)"/, a); print substr($0, a[1, "start"], a[1, "length"])}' action.log

如果不是:

awk '/pay/ {match($0, /"money":"([0-9]+)"/); split(substr($0, RSTART, RLENGTH), a, /[":]/); print a[5]}' action.log

其中一个的结果为100.而且不需要grep.

The result of either is 100. And there's no need for grep.

这篇关于如何在awk中获取regExp的子表达式值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 15:15