I have a string variable Short_name="This_is_test_string"
I want to output the substring test_string
from the Short_name
variable using sed
.
I have a string variable Short_name="This_is_test_string"
I want to output the substring test_string
from the Short_name
variable using sed
.
Like this:
echo 'Short_name="This_is_test_string"' | sed 's/.*_is_//;s/"//'
Breaking it down:
s -- substitute command
/ -- delimiter
.*_is_ -- match this
.* -- any character (.), as many as possible (*)
_is_ -- these 4 exact characters
/ -- delimiter
(nothing) -- what to replace any match with
/ -- delimiter
; -- end this command, start another
s -- substitute command
/ -- delimiter
" -- match this character
/ -- delimiter
(nothing) -- what to replace any match with
/ -- delimiter
[EDIT]
The key is figuring out which pattern matches your needs (which I don't yet have a good understanding of). Perhaps this:
... | sed 's/.*[[:<:]]is[ _]*//;s/"//'
The "tricky bit" here is [[:<:]]
which is a "zero width assertion" (in this case "beginning of word") which keeps us from doing the wrong thing on inputs like this:
echo 'string2=is this it"' | sed 's/.*[[:<:]]is[ _]*//;s/"//'
(by keeping us from matching the "is" in "this")
You don't need sed. Bash parameter expansion will do:
$ Short_name="This_is_test_string"
$ echo "${Short_name##*_is_}"
test_string
That removes everything from the beginning of the string up to and including the last "_is_"