Regular Expression match and extraction

2019-03-04 16:15发布

I am writing a shell script where in I need some help in matching and extracting pattern

ALARM_TYPE contain string 'Exception Type - timeOverThreshold Description - High CPU eHealth Alarm ID - 1000001'

I need to extract the string after Description - and before Alarm ID and store it in a new varaible ...speciafically in this example I am looking for High CPU eHealth

How can I do it simply does awk, sed works or any other simple thing.

2条回答
淡お忘
2楼-- · 2019-03-04 16:30
sed 's/.*Description\(.*\)Alarm.*$/\1/g'
查看更多
在下西门庆
3楼-- · 2019-03-04 16:50

Here's a literal translation of what you want to do using GNU grep:

var=$(grep -oP "(?<=Description - ).*(?= Alarm ID)" file)

Or with sed:

var=$(sed 's/.*Description - \(.*\) Alarm ID.*/\1/' file)

Test:

echo "$var"

Results:

High CPU eHealth

You can read more about lookahead and lookbehind assersions here. HTH.

查看更多
登录 后发表回答