Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print regexp matches using `awk`? [duplicate]

Tags:

bash

shell

awk

Is there a way to print a regexp match (but only the matching string) using awk command in shell?

like image 994
Istvan Avatar asked Mar 28 '11 23:03

Istvan


1 Answers

Yes, in awk use the match() function and give it the optional array parameter (a in my example). When you do this, the 0-th element will be the part that matched the regex

$ echo "blah foo123bar blah" | awk '{match($2,"[a-z]+[0-9]+",a)}END{print a[0]}' foo123 
like image 81
SiegeX Avatar answered Sep 21 '22 09:09

SiegeX