pio*_*211 3 string bash extract
我有一个字符串,里面有一个句子 like
a="hello my dear friend"。我想检索前两个单词(这里应该是“hello my”),因为知道单词的数量可能会有所不同。我试过了,${a%% *}但它只给了我第一个。
同样,我需要提取没有前两个词的整个句子。我怎样才能做到这一点?
您可以为此使用 BASH 数组:
# construct an array delimited by whitespace
a="hello my dear friend"
arr=($a)
# first two words
echo "${arr[@]:0:2}"
hello my
# anything after first 2 words
echo "${arr[@]:2}"
dear friend
Run Code Online (Sandbox Code Playgroud)