insert blankline after lines containing target string

In a script file, how can I add a blank line after every line that contains a certain string?

It looked like I should be able to do it with sed, but I can't seem to get the syntax right.

% cat input_file
this is the first  line of four
this is the second line of four
this is the third  line of four
this is the fourth line of four
this is the first  line of four
this is the second line of four
this is the third  line of four
this is the fourth line of four
this is the first  line of four
this is the second line of four
this is the third  line of four
this is the fourth line of four
this is the first  line of four
this is the second line of four
this is the third  line of four
this is the fourth line of four

is to become:

this is the first  line of four
this is the second line of four
this is the third  line of four
this is the fourth line of four

this is the first  line of four
this is the second line of four
this is the third  line of four
this is the fourth line of four

this is the first  line of four
this is the second line of four
this is the third  line of four
this is the fourth line of four

this is the first  line of four
this is the second line of four
this is the third  line of four
this is the fourth line of four
lhgarrettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hongjunCommented:
Answer your question later.

hongjun
0
unixpimpCommented:
Try this:

% sed 's:this is the fourth line of four:this is the fourth line of four\
> :' input_file


Replacing the target line with itself plus a newline (the \ + <cr> at the end of line 1).   If want to get fancy and use " instead of ' and try to use variable expansion rather than writing the phrase into the sed command you'll drive yourself crazy figuring out how the shell is interpreting it...

-up
0
ecwCommented:
sed -e n -e n -e n -e x -e p -e x
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

lhgarrettAuthor Commented:
> Replacing the target line with itself plus a newline

The problem I've got is that the "fourth line" changes, except for a string embedded in the line. I don't know what the entire line will look like.

Assume that the first two words and last two words can be anything. I have to find all lines containing the string "the fourth line" and then add a blank line.
0
lhgarrettAuthor Commented:
> sed -e n -e n -e n -e x -e p -e x

Sorry, can you add a little more description. Thanks  :-)
0
lhgarrettAuthor Commented:
> sed -e n -e n -e n -e x -e p -e x

The other problem I've got is I can't assume I always have exactly four lines. I must key off of the search string embedded in the line, rather than an expected sequence of lines: searching for the embedded string "fourth line" ...

% cat input_file

We are on a very
tight schedule to get this
document approved since
this might be the fourth line of the file
we are
currently performing qualification
test rehearsal.  Marty has
the fourth line of
consented to weekly meetings with
the test team to work out
the details of this
effort.  We want to support
this could be the fourth line of the stuff
test as well as LMDPS
Please get comments to me no
later than one week from now.  
another is the fourth line of more stuff
end of stuff

is to become:

We are on a very
tight schedule to get this
document approved since
this might be the fourth line of the file

we are
currently performing qualification
test rehearsal.  Marty has
the fourth line of

consented to weekly meetings with
the test team to work out
the details of this
effort.  We want to support
this could be the fourth line of the stuff

test as well as LMDPS
Please get comments to me no
later than one week from now.  
another is the fourth line of more stuff

end of stuff



0
ozoCommented:
sed -e '/fourth line/{p' -e s/.//g -e }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhgarrettAuthor Commented:
Excellent ozo!

Thanks again to all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.