Re: Insert the content of a file (with command r?)

Tim Chase <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
On 07/24/12 05:05, Laurent Le Brun wrote:
> I'd like to insert the content of a file when the input matches some regexp.
> It tried this simple script: /regexp/r file.txt
> It works fine, except that it's inserting the content of the file
> after the line matching the regexp. I'd like to insert it BEFORE
> instead.

I played around with this and am not sure whether I've found an odd
bug in GNU sed (4.2.1 on Debian stable).  I tried holding the match,
reading the file, then printing out the held match, and it seems to
do all the reading at the end of a cycle:

  ============insert.sed==============
  /regexp/{
  h       # hold the existing pattern
  s/.*//  # delete the existing match
  # read in the file
  r file.txt
  g       # return the text we held
  }
  ====================================

I used "4" for my regex and piped the output of "seq 10" into it to
insert the file.txt before the "4" line. The "4" still comes out
before the contents of file.txt does.

Am I missing something?

-tim
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.