It might be a little easier with sed
than awk
:
string="The disk 'virtual memory' also known as 'Virtual Memory' has exceeded the maximum utilization threshold of 95 Percent."
MONITOR=$(echo "$string" | sed -n "/The disk \('[^']*' also known as '[^']*'\) .*/s//\1/p")
If awk
is necessary, then:
MONITOR=$(echo "$string" | awk "/The disk '[^']*' also known as '[^']*'/ {
print \$3, \$4, \$5, \$6, \$7, \$8, \$9; } {}')
The empty braces {}
matches any line and prints nothing, so awk
only processes lines that match the regex. Note that this assumes each disk has a name with two words in it. You need to use more powerful processing (gsub
function, for example) to do regex-based substitution. This is not awk
's forte; sed
is easier to use for that task.
Both commands are set up to handle multiple lines of data interspersed with non-matching lines (but also work on single lines containing the matching information). It would also not be very difficult to just print the names between quotes on separate lines, so that you have less dissection to do afterwards (to get the two space-separated names).