user3375672
user3375672

Reputation: 3768

awk regex last occurence of character/pattern

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, /:.+$/)
}'

Upvotes: 3

Views: 5493

Answers (2)

anubhava
anubhava

Reputation: 784958

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

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

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.

Upvotes: 4

Related Questions