Reputation: 637
I have a string I would like to split into a substring and return the second subset of the result as a string. How can I do this in zshell?
I can split at a keyword and return the next word of the substring like this:
line="quick brown fox jumps over sleepy dog"
splits=${(MS)line##over*}
nextword=$splits[(w)1] #<-- returns "sleepy"
How can I get the entire substring after the split such as "sleepy dog" or "sleepy brown dog" (if the string contains more words)? In essence, what I'm looking for in zshell is the awk
equivalent of:
echo "quick brown fox jumps over sleepy dog" | awk -F'over' '{print $2};'
Upvotes: 0
Views: 452
Reputation: 531075
A slightly different approach:
[[ $line =~ "over (.*)" ]]
next_word=$match[1] # or just $match
Upvotes: 0
Reputation: 10264
You can use a negative range offset (-1
in this case) at the end of your splits
indexing. To get from sleepy
all the way up to the last element, you could just use $splits[(w)1,-1]
.
Upvotes: 2