Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我有以下字符串:
variable_A=12345variable_B=54321variable_C=24680
如何根据字符串的内容将其拆分,例如,如果我想提取“variable_B”之后的所有内容,结果将是
variable_B=54321variable_C=24680
“分隔符/字符串”将包含在最终结果中0
这是在 bash 环境中。感谢您的帮助
因此,您只想删除分隔符前面的所有内容,但将所有其他变量保留在最终输出中?
另一种纯 bash 方法删除并包括分隔符,并将分隔符添加到前面:
$ foo="variable_A=12345variable_B=54321variable_C=24680" $ foo="variable_B${foo#*variable_B}" $ echo "${foo}" variable_B=54321variable_C=24680
试试这个 :
使用grep:
$ echo 'variable_A=12345variable_B=54321variable_C=24680' | grep -o "variable_B.*"
使用sed:
$ echo 'variable_A=12345variable_B=54321variable_C=24680' | sed -r 's@.*(variable_B.*)@\1@'
输出是: