Q:

sed substitution over a range

I have file with the following entries:
abc
def
ghi
jkl
mno
pqr
stu
vwx
yz1
234
567
890

What would the sed expression to remove the trailing newline characters from it for the first 10 lines ?
Thanks.

A:

I'd say
sed '1,9 { H; d; }; 10 { x; G; s/\n//g; }' filename

What this does is:
1,9 {            # For the first nine lines:
  H              # append line to hold buffer
  d              # then discard it.
}
10 {             # in the tenth line:
  x              # swap the assembled lines in the hold buffer with the pattern
                 # space containing the tenth line
  G              # append hold buffer (holding the tenth line) to the pattern
                 # space (holding the previous nine lines)
  s/\n//g        # remove all newlines from that.
}

Alternatively,
sed '1 { N;N;N;N;N;N;N;N;N; s/\n//g; }' filename

will also work. When processing the first line, it fetches the next nine (using nine N commands), then removes the newlines between them. However, I find this less easibly adaptable and therefore less pretty.

