查找字符串中的ampersend(bash)

ont*_*oad 1 string bash escaping

我有这个字符串="dontcare noone&11111-&1111-&C00 noone"

我必须从第一个空白中提取子串&11111-&1111-&C00

我已经尝试了一些索引和一些没有任何运气的sed.

有人有一些很好的建议吗?

lar*_*sks 5

您可以使用bash的字符串操作功能:

string="dontcare noone &11111-&1111-&C00 noone"

# remove everything up to the first "&"
string="&${string#*&}"

# remove everything from the end to the earliest blank
string="${string%% *}"

# ta da!
echo $string
&11111-&1111-&C00
Run Code Online (Sandbox Code Playgroud)