0

I have code like this

echo abc | awk '$0 ~ "a\(b\)c" {print $0}'

What if I only wanted what's in the parentheses instead of the whole line? This is obviously very simplified, and there is really a lot of awk code so I don't want to switch to sed or grep or something. Thanks

user2672807
  • 1
  • 2
  • 4
  • 20

2 Answers2

3

As far as I know you cannot do it in the pattern part, you must do it inside the action part with the match() function:

echo abc | awk '{ if ( match($0, /a(b)c/, a) > 0 ) { print a[1] } }'

It yields:

b
Birei
  • 33,968
  • 2
  • 69
  • 79
  • 1
    You absolutely can do it outside of the action part: http://stackoverflow.com/questions/2957684/awk-access-captured-group-from-line-pattern/4673336#4673336 – glenn jackman Oct 06 '13 at 00:25
  • BTW, this is a GNU awk extension. – glenn jackman Oct 06 '13 at 00:25
  • @glennjackman: Good one. I didn't know you could use an array from the `pattern` to the `action. I upvoted yours in the other side. – Birei Oct 06 '13 at 00:32
  • 1
    If there's a match, the return value of `match` is true, so the action block will execute. – glenn jackman Oct 06 '13 at 00:45
  • This is the correct answer but not for msysgit which uses GNU Awk 3.0.4: `match() cannot have 3 arguments`. GnuWin32's gawk 3.1.6 seems to work fine though. I've marked your answer as correct since it appears to be correct in other cases. – user2672807 Oct 06 '13 at 19:31
  • @user2672807 Those are both very old versions of gawk, you should consider installing a newer one as you're missing a lot of very useful functionality. – Ed Morton Oct 07 '13 at 12:52
2

With GNU awk:

$ echo abc | awk '{print gensub(/a(b)c/,"\\1","")}'
b
Ed Morton
  • 157,421
  • 15
  • 62
  • 152
  • Thanks Ed, that's a good alternative. match() doesn't work for msysgit's gawk so I'll probably use something similar to what you suggested. – user2672807 Oct 06 '13 at 19:32