Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

linux

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]+) ?

like image 652
RoyHu Avatar asked Jun 06 '12 11:06

RoyHu


2 Answers

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.

like image 132
Dennis Williamson Avatar answered Nov 16 '22 00:11

Dennis Williamson


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

like image 21
Levon Avatar answered Nov 16 '22 02:11

Levon