bash: find last occurrence of a character in a string

When writing shell scripts, you usually run into use cases where you want to manipulate text strings. Here we will see how you can find the last occurence of a single character in a string and then use that information to manipulate text or strings.

For the purpose of this post, we will consider the character that we can to find as (hyphen). You can use or substitute any character instead of the hyphen to suit your requirements. Also, lets consider the string that we have to a be time stamp that looks something like this : Today 2019-06-21 02:33:44. This is just a random example and it can be any string.

There are mainly two different text manipulation commands you can use : awk and sed. There are other ways to do this but it generally involves more programming than necessary.

using awk command

The example below specifies the field separator to be “-“. awk will find the occurrences of the character “-” and will split the string based on the field separator and will print the string after the last occurrence.

bash$ echo "Today 2019-06-21 02:33:44" | awk -F "-" '{print $NF}'

This works if you want to split the string using a separator character and want to do something with the rest of the string. If you like to find the exact index of the last occurrence of the character in the string, then you use the length function in the awk command.

bash$  echo "Today 2019-06-21 02:33:44" | awk -F "-" '{print length($0)-length($NF)}'

using sed command

Although, I find the awk command to be much easier to find the occurrence of characters in string, you can use the sed command as well to do the same. In order to print out the string after the last occurrence of a character, you can do:

bash$ echo "Today 2019-06-21 02:33:44" | sed 's#.*-##g'

If you want to print out the first part of the string before the last occurrence of the character in the string, then you can modify the sed argument to do just that.

bash$ echo "Today 2019-06-21 02:33:44" | sed 's#[^-]*$##'

using the brace notation

In Linux, brace notations are a useful way to print out variables. We can use the same to print out strings before and after the last occurrence of a character. To print out the string before the last occurrence.

bash$ t='Today 2019-06-21 02:33:44' && echo ${t%-*}

To print the string after the last occurrence of the character, you can do something like

bash$ t='Today 2019-06-21 02:33:44' && echo ${t##*-}