Nik
Nik

Reputation: 201

Search String using Shell Awk

I have a string:

The disk 'virtual memory' also known as 'Virtual Memory' has exceeded the maximum utilization threshold of 95 Percent.

I need to search every time in this string word The disk and if found then I need to extract only phrase in '*' also known as '*' and put it in a variable MONITOR

In other words I want to search and put the value to

MONITOR="'virtual memory' also known as Virtual Memory'"

How can I do it using awk?

Upvotes: 0

Views: 832

Answers (2)

Wernsey
Wernsey

Reputation: 5491

Here's a snippet that does what you describe. You should put it in $(...) to assign it to the $MONITOR variable:

$ awk '/The disk '\''.*'\'' also known as '\''.*'\'' has exceeded/ {gsub(/The disk /,"");gsub(/ has exceeded.*$/,"");print}' input.txt

The two problems with awk in this case is

  • it doesn't have submatch extraction on its regexes (which is why my solution uses gsub() in the body to get rid of the first and last part of the line.
  • To use the quotes in your awk regex in a shell script you need the '\'' sequence to scape it (more info here)

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753525

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).

Upvotes: 0

Related Questions