RoyHu
RoyHu

Reputation: 333

how to get sub-expression value of regExp in awk?

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 :

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

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

Upvotes: 11

Views: 12983

Answers (5)

RoyHu
RoyHu

Reputation: 333

grep pay action.log | awk -F "\n" 'm=gensub(/.*money":"([0-9]+)".*/, "\\1", "g", $1) {print m}'

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360733

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

If not:

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

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

Upvotes: 5

Levon
Levon

Reputation: 143172

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:

awk -v FS=\" '{print $9}' data.txt

assuming data.txt contains

y1e","email":"","money":"100","coin.log

yielding:

100

I.e., your field separator is set to " and you print out field 9

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77185

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.

You can try something like this -

$ awk -v FS=[,:\"] '{ for (i=1;i<=NF;i++) if($i~/money/) print $(i+3)}' inputfile

Upvotes: 0

buckley
buckley

Reputation: 14129

You need to reference group 1 of the regex

I'm not fluent in awk but here are some other relevant questions

awk extract multiple groups from each line

GNU awk: accessing captured groups in replacement text

Hope this helps

Upvotes: 0

Related Questions