2

I have a file like below

SID_LIST_LISTENER_DG =
  (SID_LIST =
      (SID_DESC =
         (GLOBAL_DBNAME = DRCDBDB_DGMGRL)
         (ORACLE_HOME = /ofa/u1002/18C_DB_HOME)
         (SID_NAME = DRCDBDB1)
      )
      (SID_DESC =
         (GLOBAL_DBNAME = DRNONCDB_DGMGRL)
         (ORACLE_HOME = /ofa/u1002/18C_DB_HOME)
         (SID_NAME = DRNONCDB1)
      )
   )



SID_LIST_LISTENER =
  (SID_LIST =
      (SID_DESC =
         (GLOBAL_DBNAME = DRTESTDB)
         (ORACLE_HOME = /ofa/u1002/18C_DB_HOME)
         (SID_NAME = DRTESTDB1)
      )
      (SID_DESC =
         (GLOBAL_DBNAME = DRMIGDB)
         (ORACLE_HOME = /ofa/u1002/18C_DB_HOME)
         (SID_NAME = DRMIGDB1)
      )
      (SID_DESC =
         (GLOBAL_DBNAME = DRNONCDB)
         (ORACLE_HOME = /ofa/u1002/18C_DB_HOME)
         (SID_NAME = DRNONCDB1)
      )
   )

I am using the command below command to add the below lines in the file. But Since I have two matching lines as same the below line is added on both.

sed -i '/SID_LIST =/a \ \ \ \ (SID_DESC = \n \ \ \ \ \ (GLOBAL_DBNAME = DB11G) \n \ \ \ \ \ (ORACLE_HOME = /ofa/u1002/18C_DB_HOME) \n \ \ \ \ \ (SID_NAME = DB11G1) \n \ \ \ )' /tmp/listener.ora

Now I need to add only after "SID_LIST_LISTENER = (SID_LIST =" line

How to use sed to match two lines

  • I even tried like below. But it is not working sed -i '/SID_LIST_LISTENER = SID_LIST =/a \ \ \ \ (SID_DESC = \n \ \ \ \ \ (GLOBAL_DBNAME = DB11G) \n \ \ \ \ \ (ORACLE_HOME = /ofa/u1002/18C_DB_HOME) \n \ \ \ \ \ (SID_NAME = DB11G1) \n \ \ \ )' /tmp/listener.ora – Raj Nov 6 '19 at 12:46
0

You can match the first line with a simple /^... regexp, and then use a bracketed block to examine the second line.

sed '/^SID_LIST_LISTENER =$/{ N ; /\n\s*(SID_LIST =$/ a _line(s) to append _
    ; }'

N reads in the next line of input, appending to the one you already have, and /\n... matches only at the join. On all non-matches, the input will still be printed unmodified.

When trying to append lines with a\, it's probably easier to put it all into a script file:

/^SID_LIST_LISTENER =$/{ N ;
  /\n\s*(SID_LIST =$/ a\
    (SID_DESC =\
      (GLOBAL_DBNAME = DB11G)\
      (ORACLE_HOME = /ofa/u1002/18C_DB_HOME)\
      (SID_NAME = DB11G1)\
    )
  ; }

and call it with

sed -f addlines.sed

Also be aware that this routine won't work if your input contains two consecutive "first lines", ie.

SID_LIST_LISTENER =
SID_LIST_LISTENER =
  (SID_LIST =

because the first match will "use up" the second; but I don't think that's going to occur given the structure of your input.

| improve this answer | |
  • I tried this ans its not working sed -i '/^SID_LIST_LISTENER =$/{ N ; /\n\s*(SID_LIST =$/ a \ \ \ \ (SID_DESC = \n \ \ \ \ \ (GLOBAL_DBNAME = DB11G) \n \ \ \ \ \ (ORACLE_HOME = /ofa/u1002/18C_DB_HOME) \n \ \ \ \ \ (SID_NAME = DB11G1) \n \ \ \ )' /tmp/listener.ora – Raj Nov 6 '19 at 14:20
  • What was the error you got? If it was unmatched '{', then that's because you missed the second line (; }'). – JigglyNaga Nov 6 '19 at 14:24
  • Yes I tried like this and it works. But the command is two lines ... any way to do it on single line. sed -i '/^SID_LIST_LISTENER =$/{ N ; /\n\s*(SID_LIST =$/a \ \ \ \ (SID_DESC = \n \ \ \ \ \ (GLOBAL_DBNAME = DB11G) \n \ \ \ \ \ (ORACLE_HOME = /ofa/u1002/18C_DB_HOME) \n \ \ \ \ \ (SID_NAME = DB11G1) \n \ \ \ ) ; }' /tmp/listener.ora – Raj Nov 6 '19 at 14:33
  • I don't think you can put it on a single line with standard sed; the a command appends everything up to the next line end (with the option of escaping additional linebreaks with \ ), so you need to finish it, then close off the } block. Would it be acceptable to use a sed scriptfile, sed -i -f addlines.sed? – JigglyNaga Nov 6 '19 at 14:52
  • that also acceptable .. I will use this method. Thanks for the quick help – Raj Nov 6 '19 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.