return only exact match with grep

Viewed 56

All I want is the return value from the exact match of one specific line.

For example /sbin/megacli -LDInfo -Lall -aALL -NoLog | grep "Virtual Drive:" returns Virtual Drive: 0 (Target Id: 0)

The normal output is like

Adapter 0 -- Virtual Drive Information:
Virtual Drive: 0 (Target Id: 0)
Name                :
RAID Level          : Primary-5, Secondary-0, RAID Level Qualifier-3
Size                : 3.271 TB
Sector Size         : 512
Is VD emulated      : No
Parity Size         : 558.406 GB
State               : Optimal

I am only interested in that Virtual Drive number, because I iterate over it. Is there some mode like -A where the next line returns, but only the next character? My desired output is just 0 in this example.

I tried some regexes but on match, the whole line (and other unrelated) was returned. Do you have any ideas?

Thanks in advance

1 Answers

You could use instead of :

megacli .... | awk '/^Virtual Drive/ {print $3}'

awk splits each line (which is in the variable $0) on whitespace and populates the variables $1 and up. $3 is here the third "word" on the matching line.

Related