3

In python, I would do something simple like sRet = sOut.split('Word')

In bash, scrounged from other answers, I have the following two methods that are insufficient in my case, but may be useful to someone in the future:

sOut="I want this Point to matter"

1)  sRet=( $sOut )
2)  IFS="Point " read -r -a sRet <<< ${sOut}

echo ${sRet[-1]}

I want returned: "to matter"
(1) gives: "matter"
(2) gives: "er"

The first only splits by spaces, the second splits by the last character, in this case it would be 't'.

How do I split by a full string, as I would in python?

3
  • For strings manipulation, I suggest you to invoke sed (from within the Bash script, in your case). Commented Dec 7, 2015 at 15:01
  • 1
    IFS can only be single character, so will split on all letters in point. I also don't understand how you expected the first example to print after point ? Commented Dec 7, 2015 at 15:08
  • @123: Just showing what I have for future people to read this question. It may be useful and sufficient for them. I'll edit the question to make that a bit clearer. Commented Dec 7, 2015 at 15:14

5 Answers 5

3
sOut="I want this Point to matter"
s="Point "
[[ $sOut =~ $s(.*) ]] && echo ${BASH_REMATCH[1]}

Output:

to matter
Sign up to request clarification or add additional context in comments.

1 Comment

Lovely in this very specific and limited example, but useless in my broader use case
2

IFS is single character, so you will need to deploy another tool. I'd suggest awk in this case:

$ awk -F 'Point' '{print $NF}' <<< "$sOut"
 to matter

You can replace 'Point' with a variable holding the delimiter. You can also change which part of the split you get back. The variable $NF means "the last element". You can also use $1 for the first element, $2 for the second, and so on.

5 Comments

can this be expanded to print the last element, instead of the second element?
@Roman Don't follow. When you split on "Point" (in your example) there are two parts: the one before and the one after. $1 = "I want this" and $2 = "to matter". So the second is also the last, in this example.
If my sOut grows to include lots of "Point "s, then the second and the last elements are different. Your answer works, and it is pretty, just wondering how far it can go.
Ha! Just posted your newest variation as well. Not sure what the difference is, but your current answer allows for variables nicely. Thanks for all the help!
Doesn't it leave spaces before to matter?
1

You can use awk for splitting the string:

text="I want this Point to matter"
s='Point'
awk -v s="$s" -v text="$text" 'BEGIN {split(text, a, "[[:blank:]]*" s "[[:blank:]]*");
    for (i in a) print a[i]}'
I want this
to matter

To get only the last match:

awk -v s="$s" -v text="$text" 'BEGIN {n=split(text, a, "[[:blank:]]*" s "[[:blank:]]*"); print a[n]}'
to matter

Or:

awk -v s="$s" 'BEGIN{FS="[[:blank:]]*" s "[[:blank:]]*"} {print $NF}' <<< "$text"
to matter

IFS on the other hand doesn't work with multiple character string. So IFS='Point' will split the output on each character P, o, i, n, t.

Comments

1
sDelim="Point"
sRet1=$(awk -F ${sDelim} '{print $1}' <<< ${sOut})
sRet2=$(awk -F ${sDelim} '{print $NF}' <<< ${sOut})

Given all the other excellent answers, I prefer this one most for the following reasons:
1) Its short ans sweet
2) Everything is fairly explicit when wanting to use variables
3) Any elements can be selected: 1,2,.. from the beginning, NF, NF-1,.. from the end
4) if sDelim is not actually in sOut, the script doesn't freak out

Thanks mainly to @bishop for leading me to this

1 Comment

Don't forget to quote ${sDelim} and ${sOut}, otherwise they are subject to word splitting and globbing.
-1

You could use the parenthesis feature of sed to retrieve the string that is matched.

The below code:

sOut="I want this point to matter"
s="point "
echo $sOut | sed "s/.*$s\(.*\)/\1/"

would give me:

to matter

as output.

1 Comment

May I know the reason for the downvote? Any test cases where this won't work? Or is it that this can be solved even without using a stream editor like how Cyrus did it in the other answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.