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, ie 3). Explanations really appreciated.
echo 12:45:78 |
awk '
{
print match($1, /:.+$/)
}'
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.