Author Topic: [shell] Get last part of a string ?  (Read 4709 times)

0 Members and 1 Guest are viewing this topic.

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
[shell] Get last part of a string ?
« 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
« Last Edit: December 07, 2013, 11:55:26 am by Hayleia »
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline SpiroH

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +153/-23
    • View Profile
Re: [shell] Get last part of a string ?
« Reply #1 on: December 07, 2013, 01:14:19 pm »
Question: Is this supposed to be solved using shell scripts only?

Offline Hooloovoo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 225
  • Rating: +22/-0
    • View Profile
Re: [shell] Get last part of a string ?
« Reply #2 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.
« Last Edit: December 07, 2013, 01:26:04 pm by fortytwo »
"My world is Black & White. But if I blink fast enough, I see it in Grayscale." -tr1p1ea
Spoiler For some of the calcs I own:



(actually I have quite a few more than this, but I don't feel like making bars for them all.)

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: [shell] Get last part of a string ?
« Reply #3 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
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s