regular expression – not understanding regex pattern for matching an integer

(First, this is not a bash integer regex pattern. sed is handling the regex and has nothing to do with bash)

So I understand that you are unable to figure out the regex in sed -ne 's/^.* ([0-9]{1,99})/1/p'

Let’s make it simpler. The equivalent code would be sed -E -ne 's/^.* ([0-9]{1,99})/1/p'

The -E enables extended (modern) regex and you won’t have to escape stuff using

The s/pattern/replacement/ operator looks for a string matching the regex pattern in the left side and replaces it with the string on the right.

Now, let’s focus on /^.* ([0-9]{1,99})/

  • ^ matches at the beginning of the line
  • .* matches any character 0 or more times
  • (space) matches a single space character
  • [0-9]{1,99} matches any digits occurring a minimum of 1 time to a max of 99; so any number with a length of 1 to 99

([0-9]{1,99}) is in parentheses which means sed will “capture” this substring and use it in the replacement part where 1 was used.

Read more here: Source link