40

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

Community
  • 1
  • 1
Istvan
  • 7,500
  • 9
  • 59
  • 109

3 Answers3

47

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
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • 2
    I know grep -o, it has to be in AWK :) – Istvan Mar 29 '11 at 00:03
  • 6
    I get the error: awk: syntax error at source line 1 context is >>> {match($2,"[a-z]+[0-9]+", <<< awk: illegal statement at source line 1 awk: illegal statement at source line 1 with both zsh and bash – dentarg Aug 31 '14 at 20:29
  • 8
    oh, it's gawk... from http://stackoverflow.com/questions/5536018/how-to-get-match-regex-pattern-using-awk-from-file "the awk match() function with three arguments only exists in gawk" – dentarg Aug 31 '14 at 20:31
  • 3
    So in other words, this doesn't answer the question. – Joshua Cook Oct 30 '15 at 14:32
  • 1
    For those interested in using this gawk-specific match function, it looks like the 3-arg version of match wasn't added to gawk until 3.1: https://www.gnu.org/software/gawk/manual/html_node/Feature-History.html Sadly, the latest Git Bash's environment is using 3.0.4. – Tyler Hoppe Mar 14 '16 at 16:51
  • 1
    gawk not installed by default in MacOS. Refer to the solution below for wider compatibility implementation using standard awk – MarcSitges Jun 22 '18 at 14:17
27

An awk specific (as opposed to one using gawk) implementation of the solution:

$ echo "blah foo123bar blah" | awk 'match($0,/[a-z]+[0-9]+/) {print substr($0,RSTART,RLENGTH)}'
foo123
kenorb
  • 155,785
  • 88
  • 678
  • 743
Joshua Cook
  • 12,495
  • 2
  • 35
  • 31
2

I use this construct quite a bit:

split(substr($0, match($0, /[0-9]+ [Bb]ytes/)), a, " ");
print a[1];
kenorb
  • 155,785
  • 88
  • 678
  • 743
nyet
  • 482
  • 2
  • 13