2

Very simple task: extract the region from an AWS arn.

Example:

arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

I need to extract eu-west-2

I have a working regex for this: ^(?:[^:]+:){3}([^:]+).*

I tried this command, but it returns the entire string:

echo "arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3" | grep -oP '^(?:[^:]+:){3}([^:]+).*'

output: arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3

What is wrong with the above?

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
Kappacake
  • 1,826
  • 19
  • 38

3 Answers3

2

Thanks for @Inian for the quick and good answer in the comments:

echo "arn:aws:lambda:eu-west-2:12345678912:layer:my-awsome-layer:3" | cut -d':' -f4

Does the trick.

output: eu-west-2

Kappacake
  • 1,826
  • 19
  • 38
2

A more flexible approach:

grep -oP '\w{2}-\w+-\d+'
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
2

You get back the whole line because you are using .* at the end of the pattern.

As you are using grep -oP you can make use of \K to forget what is matched so far:

grep -oP '^(?:[^:]+:){3}\K[^:]+'

An alternative using awk setting : as the field separator and printing the fourth field:

awk -F: '{print $4}'

Or using sed and replace with capture group 2, as capture group 1 is repeated 3 times:

sed 's/^\([^:]\+:\)\{3\}\([^:]\+\).*/\2/'

The examples will output:

eu-west-2
The fourth bird
  • 154,723
  • 16
  • 55
  • 70