如何削减从bash shell的字符串

问题描述:

从Linux的Bash可以帮我如何只显示的如何削减从bash shell的字符串

Monitoringmetric=1.5Count 

代替

Monitoringmetric=1.5Count;1;2 
+0

'echo“Monitoringmetric = 1.5Count; 1; 2”| cut -f1 -d“;”' –

+1

您可以使用bash:'s ='Monitoringmetric = 1.5Count; 1; 2';回声“$ {s %%; *}”' – anubhava

+0

感谢让和anubhva,这两个建议像魅力 我可以再问一个我怎么才能只剪切和只显示= 1.5 –

既然你似乎只关心捕捉Monitoringmetric =后的数字,你可以这样做:

echo "Monitoringmetric=1.5Count;1;2" | sed -n -e 's/^Monitoringmetric=\([0-9.]*\).*/\1/p' 

终于露面,但似乎是一个很长的命令,请告知我是否有办法缩短这一点。

$ echo“Monitoringmetric = 1.5Count; 1; 2”| cut -f2 | sed -e's/Monitoringmetric = // g'| cut -f1 -d“;” | SED -e“S /计数// G”

+0

你为什么要做** cut -f2 **,这似乎并没有完成任何事情?你想从输入中提取什么?只是** 1.5 **? – blackpen

如果字符串是在一个变量,您还可以使用$ {a %%; *},例如:

a="Monitoringmetric=1.5Count;1;2" 
echo ${a%%;*}