2

I need to get the index of the last occurrence of a pattern. Tried ideas from here and here. The below does not work if I want the index of the last : (index is 6). Tried to use the anchor $ but clearly have not grasped it (since it gives me the first occurrence, i.e. 3). Explanations really appreciated.

echo 12:45:78 | 
awk '
{
print match($1, /:.+$/)
}'
Community
  • 1
  • 1
user3375672
  • 3,728
  • 9
  • 41
  • 70

2 Answers2

3

You need to use

/:[^:]*$/

Here, [^:]* (a [^...] is a negated bracket expression) will match 0+ chars other than :, so, only the last : is matched with the first :.

Note this idea is almost right for you, the only difference being the quantifier: if you use + with [^:], you won't match the : that is at the very end of the input string. Thus, * is the quantifier you want.

Pattern details:

  • : - a : followed with...
  • [^:]* - any 0+ chars other than :
  • $ - end of string.
Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

There is no need to use any regex here as awk allows you split file using a delimiter.

After using : as input field separator subtract last field's length from total line length to get right index like this:

awk -F: '{print length($0) - length($NF)}' <<< '12:45:78'

6

More examples:

awk -F: '{print length($0) - length($NF)}' <<< '12:45:78111:123'
12

awk -F: '{print length($0) - length($NF)}' <<< '12:45:78:123'
9

awk -F: '{print length($0) - length($NF)}' <<< '12:45'
3

awk -F: '{print length($0) - length($NF)}' <<< '12:'
3
anubhava
  • 761,203
  • 64
  • 569
  • 643