Re: print specific line (by address) within a block of text

Davide Brini <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
On Wed, 12 Dec 2012 13:13:10 -0500, "Brian J. Murrell"
<[email protected]> wrote:

> Hi,
> 
> So I have some text in which there is a block, delimited by a pattern
> and within that block I want to print a line of text by it's line
> number, relative to the start of the block.  So, given the input:
> 
> a
> b
> c
> d
> e
> f
> g
> h
> 
> i
> j
> k
> l
> m
> 
> within the block starting at 'd' and ending with the blank like I want
> to print the third line, so in this case the line with the "f" on it.
> 
> I am constrained to matching the /d/,/^$/ block by patterns and the
> block could be anywhere in a file so I won't know any line numbers ahead
> of time.
> 
> Extracting the block is simple enough:
> 
> /d/,/^$/
> 
> but once I have that block how can I address into it relative the the
> first line of the block since the block still has it's absolute line
> numbers.
> 
> i.e. /d/,/^$/{
> =
> }
> 
> will print:
> 4
> 5
> 6
> 7
> 8
> 9
> 
> And of course the beginning pattern will not always occur on the same
> line.
> 
> I tried a hold space and replacing the pattern space with the hold space
> but that seemed to retain the original line number addressing once it
> was brought into the pattern space.

Assuming that /d/ *always* starts a block, and omitting some nitpicks
that perhaps are unlikely to be important, you could do

sed -n '/d/{n;n;p;}'

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