1

I have a text like this:

ora.sas.aa.svc
      1        ONLINE  ONLINE       sas1                                         
ora.sas.bb.svc
      1        ONLINE  ONLINE       sas2                                         
ora.sas.cc.svc
      1        ONLINE  ONLINE       sas2                                         
ora.sas.dd.svc
      1        ONLINE  ONLINE       sas1                                         
ora.sas.ee.svc
      1        ONLINE  ONLINE       sas1                                         
      2        ONLINE  ONLINE       sas2  

What i want is to grep specific part of this .svc and online status like:

example.txt |grep aa
  ora.sas.aa.svc
          1        ONLINE  ONLINE       sas1     

or

example.txt |grep ee
 ora.sas.ee.svc
      1        ONLINE  ONLINE       sas1                                         
      2        ONLINE  ONLINE       sas2  

row number of "online" may change, so i can't grep with grep -A2 or grep -A3.

3 Answers 3

2

You can use awk instead:

awk -v s='ee' 'NF==1 && $0 ~ s{p=1} NF==1 && !($0 ~ s){p=0} p' file
ora.sas.ee.svc
      1        ONLINE  ONLINE       sas1
      2        ONLINE  ONLINE       sas2

awk -v s='aa' 'NF==1 && $0 ~ s{p=1} NF==1 && !($0 ~ s){p=0} p' file
ora.sas.aa.svc
      1        ONLINE  ONLINE       sas1
Sign up to request clarification or add additional context in comments.

Comments

2

Not sure if you can do it with grep, but you can definitely do it with awk.

<example.txt awk 'NF==1 { got_match = ($0 ~ /PATTERN/)} got_match {print}'

Replace PATTERN with "ee" or "aa" and it should do the trick.

Comments

1

With sed:

$ func() {
> sed -n "/$1/{:a;p;n;/^[[:space:]]/ba}" "$2"
> }
$ func aa example.txt
ora.sas.aa.svc
      1        ONLINE  ONLINE       sas1
$ func ee example.txt
ora.sas.ee.svc
      1        ONLINE  ONLINE       sas1
      2        ONLINE  ONLINE       sas2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.