Geek Geek - 1 year ago 87
Bash Question

Can we use regex within sed

I have a sample file with many sections. There can be multiple new lines within a section. So, we can encounter multiple new lines (\n) within a section.


(a) (b)
(c) (d)
(e) (f)

Section 3

I want to replace all the contents of Section 2 with some other content. So, one thing I am aware is that section closes with consecutive ) brace. Is there a way I can do it via a
using regex. I want to do it via only shell script only.

I know I can give


How do I add a
here to populate
. Can I give a full fledged regex here?

Expected Output


(m) (n)
(o) (p)
(q) (r)

Section 3

Only Section 2 contents need to be found first and then it needs to be replaced with some content like above.

Answer Source

With the c(change) command:

sed '
/Section2/,/))/c \
    Section2 \
    ( \
        (m)   (n) \
        (o)   (p) \
        (q)   (r) \
' file
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download