Re: sed 4.2.1 bug

Paolo Bonzini <[email protected]>
Newsgroups gmane.comp.gnu.utils.bugs
Message-ID <[email protected]>
Il 10/07/2012 12:26, Sophia Jacob ha scritto:
> Hi,
> I stumbled upon a possible bug in sed:
> 
> $ cat c
> 1
> 2
> 3
> 4
> 5
> 
> $ sed -n 'H;4,${g;D;h}' c   # print the last 3 lines of a file
> sed: couldn't re-allocate memory
> 
> Basically, if 'D' is used after 'g' or 'G' or 'x' (after copying from
> the hold space), this realloc error is thrown. I think this is a bug.
> Please let me know if this is a usage error on my part.

the D command automatically restarts the script (i.e. it is followed by
an implicit b<first-command-in-the-script>.

The effect you want can be obtained with the following command:

   sed '1{;$!N;$!N;};$!N;$!D'

where the number of printed lines is equal to the total number of N
commands.  You can also avoid repeated $!N commands with a loop, like this:

  :x
  $q
  $!N
  4,$D
  bx

which will print the last 3 lines too.

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.