MSK
MSK

Reputation: 179

Multiple regexes with awk

I have a file. I have to apply multiple regexes one by one in Linux with AWK command. Here is my example File:

Start connection Test from LAN end
Link are Test is complete available
Test    
Start connection from LAN 
Test is complete
end
Test1 is complete
Test2 is complete
Link are available
Link are Test is complete available
Test1

I want to apply 3 rules:

  1. dont see between Start and end
  2. dont see between Link and available
  3. see Test

I have used three AWK awk '/Start/ {f=1} !f; /end/ {f=0}' , awk '/Link/ {f=1} !f; /available/ {f=0}' and awk '/Test/{f=1}f;/complete/{f=0}' Now i want to combine all together in one AWK.

How can i do that?

Upvotes: 1

Views: 182

Answers (2)

anubhava
anubhava

Reputation: 785128

This single awk should work:

awk '{
   gsub(/Start.*end/, "");
   gsub(/Link.*available/, "");
   split($0, a, "\n");
   for (i=0; i<length(a); i++)
      if (index(a[i], "Test"))
         print a[i]
}' RS= file

Upvotes: 1

SheetJS
SheetJS

Reputation: 22905

Just use different dummy variables:

awk '/Start/ {f=1} /Link/ {g=1} /Test/ {h=1} !f && !g && h; /end/ {f=0} /available/ {g=0} /complete/ {h=0}'

Upvotes: 5

Related Questions