You're viewing a comment by Unix User and its responses.

Unix User Permalink
October 24, 2008, 00:20

Something I often need to do is match lines against a regexp, and print out a matching group within that line. But I have never been able to find a way to do this in awk, and end up resorting to Perl.

So - is there a way to do something like this?

/abc([0-9]+)def/ { print group(1); }

so that input of:

abc654def

produces:

654

Thanks!

Comment Responses

September 19, 2011, 07:32

you can install gawk,and do like this:
Syntax´╝Ü
gawk 'mathc($0,regex,array){print array[1]}' file

example:
gawk 'match($0, "https?:\/\/(.*?google.*?)/", array) {print array[1]}' filename

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "cdrom_100": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.