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?
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!
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.
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).
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
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"
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With