Sed. How change line next to specific pattern

My file is:

DIVIDER  
Sometext_string

many  
lines  
of random  
text  
DIVIDER  
Another_Sometext_string  
many  
many  
lines  
DIVIDER  
Third_sometext_string  
....

How change lines following DIVIDER pattern

Result must be:

DIVIDER  
[begin]Sometext_string[end]

many 
lines  
of random  
text  
DIVIDER  
[begin]Another_Sometext_string[end]

many  
many  
lines  
DIVIDER  
[begin]Third_sometext_string[end]

....

Answers


May be this would help -

sed '/DIVIDER/{n;s/.*/[begin]&[end]\n/;}' file1

Execution:

[jaypal:~/Temp] cat file1
DIVIDER
Sometext_string

many
lines
of random
text
DIVIDER
Another_Sometext_string
many
many
lines
DIVIDER
Third_sometext_string

[jaypal:~/Temp] sed '/DIVIDER/{n;s/.*/[begin]&[end]\n/;}' file1
DIVIDER
[begin]Sometext_string[end]


many
lines
of random
text
DIVIDER
[begin]Another_Sometext_string[end]

many
many
lines
DIVIDER
[begin]Third_sometext_string[end]

UPDATE:

This version will handle one blank line after the first DIVIDER.

[jaypal:~/Temp] sed -e '0,/DIVIDER/{n;s/.*/[begin]&[end]/;}' -e '/DIVIDER/{n;s/.*/[begin]&[end]\n/;}' file1
DIVIDER
[begin]Sometext_string[end]

many
lines
of random
text
DIVIDER
[begin]Another_Sometext_string[end]

many
many
lines
DIVIDER
[begin]Third_sometext_string[end]

[jaypal:~/Temp] 

UPDATE 2:

There are no other questions right now so that I thought I'd offer an alternate awk solution if you'd like? :)

awk '/DIVIDER/{print;getline;sub(/.*/,"[begin]&[end]");print;next}1' file1

[jaypal:~/Temp] awk '/DIVIDER/{print;getline;sub(/.*/,"[begin]&[end]\n");print;next}1' file1
DIVIDER
[begin]Sometext_string[end]


many
lines
of random
text
DIVIDER
[begin]Another_Sometext_string[end]

many
many
lines
DIVIDER
[begin]Third_sometext_string[end]

[jaypal:~/Temp] 

This to handle first blank line after DIVIDER -

[jaypal:~/Temp] awk '/DIVIDER/{count++;print;getline;if(count==1) sub(/.*/,"[begin]&[end]");else sub(/.*/,"[begin]&[end]\n");print;next}1' file1
DIVIDER
[begin]Sometext_string[end]

many
lines
of random
text
DIVIDER
[begin]Another_Sometext_string[end]

many
many
lines
DIVIDER
[begin]Third_sometext_string[end]

[jaypal:~/Temp] 

Need Your Help

How to become a great open source programmer of python?

python c

I'm a C# programmer of web applications.

Find CSS path in one page checkout extension in magento?

css magento

I am using one page checkout extension,and my firebug is displaying this path..