Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the characters after the last index of a substring from a string

Tags:

linux

bash

unix

I have a string which is an output of another command. I only need the end of this string to display. The separator string is "." (dot and space), and I need the string after the last index of ".".

How can I do this in Bash?

like image 804
Patartics Milán Avatar asked Mar 21 '13 12:03

Patartics Milán


People also ask

How do I get the string after the last dot?

Split a String by the last Dot in JavaScript # To split a string by the last dot, call the lastIndexOf() method to get the last index of a dot in the string and use the slice() method to get two substrings - one including the characters before the last dot, and the other - the characters after the last dot. Copied!

How do I get the last 5 characters of a string?

To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string. Copied! const str = 'Hello World'; const last3 = str.

How do you find the last character in a string index?

The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string. The lastIndexOf() method searches the string from the end to the beginning. The lastIndexOf() method returns the index from the beginning (position 0).


4 Answers

If the string is in a variable:

$ foo="header. stuff. more stuff"
$ echo "${foo##*. }"
more stuff

If there are multiple instances of ". " (as in my example) and you want everything after the first occurrence, instead of the last, just use one #:

$ echo "${foo#*. }"
stuff. more stuff
like image 187
chepner Avatar answered Oct 10 '22 17:10

chepner


try this:

your cmd...|sed 's/.*\. //'

this works no matter how many "dot" or "dot and space" do you have in your input. it takes the string after the last "dot and space"

like image 29
Kent Avatar answered Oct 10 '22 16:10

Kent


Awk is elegant weapon...for a more civilized age:

[cpetro01@h ~]$ echo "this. is. my. string. of. some. arbitrary. length" | awk -F'. ' ' { print $NF } '
length
[cpetro01@h ~]$ echo "this. is. my. string. of. some" | awk -F'. ' ' { print $NF } '   
some

In this case NF is the awk variable for "Number of fields" and this construct says "print the entry in highest number of fields found" so if the size of your input changes from one line to the next you're still going to get the last one.

You can also do math:

[cpetro01@h~]$ echo "this. is. my. string. of. some. arbitrary. length" | awk -F'. ' ' { print $(NF-2) } '
some
[cpetro01@h~]$ echo "this. is. my. string. of. some. arbitrary. length" | awk -F'. ' ' { print $(NF-3) } '
of
[cpetro01@h~]$

(Yes, this is 3 years late for the OP, but one of my cow-orkers pointed me to this page today for something we were working on, so I thought I'd drop this here in case others are looking too.)

like image 38
Petro Avatar answered Oct 10 '22 16:10

Petro


Try this:

echo "This is a sentence. This is another sentence" | rev | cut -d "." -f1 | rev

The rev reverses the output. The -d specifies the delimiter, breaking everything up into fields. The -f specifies the fields you want to use. We can select f1, because we reversed the data. We don't need to know how many fields there are in total. We just need to know the first. At the end, we reverse it again, to put it back in the right order.

like image 31
Paul Calabro Avatar answered Oct 10 '22 16:10

Paul Calabro