string 在字符后获取字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15148796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:49:15  来源:igfitidea点击:

Get string after character

stringbashextract

提问by user788171

I have a string that looks like this:

我有一个看起来像这样的字符串:

 GenFiltEff=7.092200e-01

Using bash, I would like to just get the number after the =character. Is there a way to do this?

使用 bash,我只想获取=字符后的数字。有没有办法做到这一点?

回答by chepner

Use parameter expansion, if the value is already stored in a variable.

如果值已存储在变量中,则使用参数扩展。

$ str="GenFiltEff=7.092200e-01"
$ value=${str#*=}

Or use read

或使用 read

$ IFS="=" read name value <<< "GenFiltEff=7.092200e-01"

Either way,

无论哪种方式,

$ echo $value
7.092200e-01

回答by Tuxdude

For the text after the first =and before the next =

对于第一个之后=和下一个之前的文本=

cut -d "=" -f2 <<< "$your_str"

or

或者

sed -e 's#.*=\(\)##' <<< "$your_str"

For all text after the first =regardless of if there are multiple =

对于第一个之后的所有文本,=无论是否有多个=

cut -d "=" -f2- <<< "$your_str"

回答by Greg Guida

echo "GenFiltEff=7.092200e-01" | cut -d "=" -f2 

回答by jman

This should work:

这应该有效:

your_str='GenFiltEff=7.092200e-01'
echo $your_str | cut -d "=" -f2

回答by Explosion Pills

${word:$(expr index "$word" "="):1}

that gets the 7. Assuming you mean the entire rest of the string, just leave off the :1.

得到7. 假设您的意思是字符串的其余部分,只需去掉:1.