Omnimaga

General Discussion => Technology and Development => Computer Usage and Setup Help => Topic started by: Hayleia on December 07, 2013, 11:54:46 am

Title: [shell] Get last part of a string ?
Post by: Hayleia on December 07, 2013, 11:54:46 am
First of all, what does "last part" mean ? It means "all characters after some special character". For example, if that character is "-", then the "last part" of "1574-114687-415" is "415" and the "last part" of "jg7e;f,k-e" is "e".

So, yeah, how do I extract the "last part" of a string (you can provide examples with the "-") ?

edit that's surely not informative, but I am using Ubuntu 13.04
Title: Re: [shell] Get last part of a string ?
Post by: SpiroH on December 07, 2013, 01:14:19 pm
Question: Is this supposed to be solved using shell scripts only?
Title: Re: [shell] Get last part of a string ?
Post by: Hooloovoo on December 07, 2013, 01:24:42 pm
I did this using sed, a stream editor.
Code: [Select]
echo  "1574-114687-415" | sed s/.*-// will replace everything at the beginning of a string that has a - at the end with nothing, and continue on in this way. It returns everything which doesn't have a - after it.
Title: Re: [shell] Get last part of a string ?
Post by: Hayleia on December 07, 2013, 02:06:09 pm
Question: Is this supposed to be solved using shell scripts only?
Yes.

I did this using sed, a stream editor.
Code: [Select]
echo  "1574-114687-415" | sed s/.*-// will replace everything at the beginning of a string that has a - at the end with nothing, and continue on in this way. It returns everything which doesn't have a - after it.
Thanks, that works great :D