Nik
Nik

Reputation: 201

Regular Expression match and extraction

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.

Upvotes: 0

Views: 101

Answers (2)

Vijay
Vijay

Reputation: 67291

sed 's/.*Description\(.*\)Alarm.*$/\1/g'

Upvotes: 0

Steve
Steve

Reputation: 54572

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.

Upvotes: 1

Related Questions