击:一个字符串中提取部分 [英] Bash : extracting part of a string

查看:156
本文介绍了击:一个字符串中提取部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有字符串内存使用:19.54M
我将如何从中提取了19.54?
所以我需要将其存储在一个变量,并与下一次迭代的值进行比较的19.54会经常发生变化。

Say I have the string "Memory Used: 19.54M" How would I extract the 19.54 from it? The 19.54 will change frequently so i need to store it in a variable and compare it with the value on the next iteration.

我想我需要的grep和正则表达式的某种组合,但我从来没有真正理解正则表达式。

I imagine I need some combination of grep and regex, but I never really understood regex..

推荐答案

您可能想的提取物的它,而不是的删除的它。您可以使用参数扩展来提取值:

You probably want to extract it rather than remove it. You can use the Parameter Expansion to extract the value:

var="Memory Used: 19.54M"
var=${var#*: }            # Remove everything up to a colon and space
var=${var%M}              # Remove the M at the end

需要注意的是bash的只能比较整数,它没有浮点算术支持。

Note that bash can only compare integers, it has no floating point arithmetics support.

这篇关于击:一个字符串中提取部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆