Linux 如何在awk中获取regExp的子表达式值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10913598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get sub-expression value of regExp in awk?
提问by RoyHu
I was analyzing logs contains information like the following:
我正在分析日志包含如下信息:
y1e","email":"","money":"100","coi
I want to fetch the value of money, i used 'awk' like :
我想获取金钱的价值,我使用了“awk”,例如:
grep pay action.log | awk '/"money":"([0-9]+)"/' ,
then how can i get the sub-expression value in ([0-9]+) ?
那么如何获得 ([0-9]+) 中的子表达式值?
采纳答案by Paused until further notice.
If you have GNU AWK (gawk
):
如果您有 GNU AWK ( gawk
):
awk '/pay/ {match(awk '/pay/ {match(awk -v FS=\" '{print }' data.txt
, /"money":"([0-9]+)"/); split(substr(y1e","email":"","money":"100","coin.log
, RSTART, RLENGTH), a, /[":]/); print a[5]}' action.log
, /"money":"([0-9]+)"/, a); print substr(100
, a[1, "start"], a[1, "length"])}' action.log
If not:
如果不:
$ awk -v FS=[,:\"] '{ for (i=1;i<=NF;i++) if($i~/money/) print $(i+3)}' inputfile
The result of either is 100
. And there's no need for grep
.
两者的结果都是100
。而且没有必要grep
。
回答by buckley
You need to reference group 1 of the regex
您需要引用正则表达式的第 1 组
I'm not fluent in awk but here are some other relevant questions
我不精通 awk,但这里有一些其他相关问题
awk extract multiple groups from each line
GNU awk: accessing captured groups in replacement text
Hope this helps
希望这可以帮助
回答by Levon
Offered as an alternative, assuming the data format stays the same once the lines are grep'ed, this will extract the money field, not using a regular expression:
作为替代方案提供,假设数据格式在行被 grep'ed 后保持不变,这将提取货币字段,而不是使用正则表达式:
grep pay action.log | awk -F "\n" 'm=gensub(/.*money":"([0-9]+)".*/, "\1", "g", ) {print m}'
assuming data.txt contains
假设 data.txt 包含
##代码##yielding:
产生:
##代码##I.e., your field separator is set to "
and you print out field 9
即,您的字段分隔符设置为"
并打印出字段 9
回答by jaypal singh
If you have money
coming in at different places then may be it would not be a good idea to hard code the positional parameter.
如果您money
在不同的地方进入,那么对位置参数进行硬编码可能不是一个好主意。
You can try something like this -
你可以试试这样的——
##代码##