[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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
0
lhgarrett
Asked:
lhgarrett
1 Solution
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
lhgarrettAuthor Commented:
Excellent ozo!

Thanks again to all.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now