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

Paolo Bonzini <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
Il 25/07/2012 08:59, Davide Brini ha scritto:
> "The a and r commands schedule text for later output. The text specified
> for the a command, and the contents of the file specified for the r
> command, shall be written to standard output just before the next attempt
> to fetch a line of input when executing the N or n commands, or when
> reaching the end of the script. If written when reaching the end of the
> script, and the -n option was not specified, the text shall be written
> after copying the pattern space to standard output. The contents of the
> file specified for the r command shall be as of the time the output is
> written, not the time the r command is applied. The text shall be output in
> the order in which the a and r commands were applied to the input."
> 
> As for the OP's problem, this should work, assuming the pattern never
> occurs on the very last line of the file:
> 
> /pattern/ {
>   r file.txt
>   N
> }
> 
> Given the way "r" works, I'm not sure there's even a way to insert the file
> before the last line of the file with sed only. In that case, one can
> perhaps use dirty shell tricks, like eg
> 
> { cat input.txt; echo; } | sed program.sed | head -n -1

Would it make sense to make the "a" command put the current pattern
space on the append ring, like this?

/pattern/ {
  r file.txt
  a
  d
}

Similarly, "A" would put the cufirst line of pattern space on the append
ring.

Opinions?

Paolo
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.