-3

I have multiple s3 buckets in AWS whose names are in the following syntax:

  1. resource-4511-deployment-1srsi6fjy9uuk
  2. web-4533-logbucket-dogx6k0n8967
  3. pcnfile6511
  4. 5399-bucket-6dehb5uuiwd

I'd like to extract the 4 digit number from each of these names preferably without using multiple if else loops which is the solution I can think of right now. The output should basically be

  • 4511
  • 4533
  • 6511
  • 5399

4 Answers4

2

You can use parameter expansion. Prefix and suffix removal return the strings before and after the four digits, you can then use the removal again to remove the prefix and suffix:

#!/bin/bash

for name in resource-4511-deployment-1srsi6fjy9uuk \
            web-4533-logbucket-dogx6k0n8967 \
            pcnfile6511 \
            5399-bucket-6dehb5uuiwd
do
    after=${name#*[0-9][0-9][0-9][0-9]}
    before=${name%%[0-9][0-9][0-9][0-9]*}
    num=${name#$before}
    num=${num%$after}
    echo $num
done
choroba
  • 231,213
  • 25
  • 204
  • 289
1

I'd use regex matching here.

I was hoping the pattern would be cleaner, but the data forces this:

re='(^|[^[:digit:]])([[:digit:]]{4})($|[^[:digit:]])'

start of string or a non-digit
followed by 4 digits
followed by end of string or a non-digit

for name in resource-4511-deployment-1srsi6fjy9uuk \
            web-4533-logbucket-dogx6k0n8967 \
            pcnfile6511 \
            5399-bucket-6dehb5uuiwd
do
    [[ $name =~ $re ]] && echo ${BASH_REMATCH[2]}
done
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

Assuming there's only one set of 4-digits in each string, one bash idea using a regex and the BASH_REMATCH[] array:

regex='([0-9]{4})'

for string in resource-4511-deployment-1srsi6fjy9uuk web-4533-logbucket-dogx6k0n8967 pcnfile6511 5399-bucket-6dehb5uuiwd
do
    [[ "${string}" =~ $regex ]] && echo "${BASH_REMATCH[1]}"
done

This generates:

4511
4533
6511
5399
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
0
printf "pcnfile6511" | grep "[0-9][0-9][0-9][0-9]"

That seems to work, although it only will for four digit numbers.

Also...

printf "resource-4511-deployment-1srsi6fjy9uuk" | cut -d'-' -f2

That will work when you have delimiters.

For numbers at the end of a line...

printf "pcnfile6511" | tail -c 4

And for numbers at the beginning...

printf "5399-bucket-6dehb5uuiwd" | head -c 4
petrus4
  • 616
  • 4
  • 7