Thursday, 15 July 2010

scripting - Search String using Shell Awk -



scripting - Search String using Shell Awk -

i have string:

the disk 'virtual memory' known 'virtual memory' has exceeded maximum utilization threshold of 95 percent.

i need search every time in string word the disk , if found need extract phrase in '*' known '*' , set in variable monitor

in other words want search , set value to

monitor="'virtual memory' known virtual memory'"

how can using awk?

here's snippet describe. should set in $(...) assign $monitor variable:

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

the 2 problems awk in case is

it doesn't have submatch extraction on regexes (which why solution uses gsub() in body rid of first , lastly part of line. to utilize quotes in awk regex in shell script need '\'' sequence scape (more info here)

shell scripting awk

No comments:

Post a Comment