Discussion:
print specific line (by address) within a block of text
Brian J. Murrell
2012-12-12 18:13:10 UTC
Permalink
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.

Any ideas on how to achieve this?

Cheers,
b.



[Non-text portions of this message have been removed]
Davide Brini
2012-12-12 22:14:04 UTC
Permalink
On Wed, 12 Dec 2012 13:13:10 -0500, "Brian J. Murrell"
Post by Brian J. Murrell
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
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.
/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/,/^$/{
=
}
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.
Brian J. Murrell
2012-12-12 22:55:43 UTC
Permalink
Post by Davide Brini
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;}'
Ahh, nice. What if the offset into the block can be variable, from only
a few lines to maybe a few dozen lines, so some way to give "n" a number
of iterations. I have taken notice of the looping abilities of sed but
have not come across any way to control loop iteration count with
numerical values.

Cheers,
b.




[Non-text portions of this message have been removed]
Thierry Blanc
2012-12-13 05:39:48 UTC
Permalink
Post by Brian J. Murrell
Post by Davide Brini
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;}'
Ahh, nice. What if the offset into the block can be variable, from only
a few lines to maybe a few dozen lines, so some way to give "n" a number
of iterations. I have taken notice of the looping abilities of sed but
have not come across any way to control loop iteration count with
numerical values.
Cheers,
b.
sed -rn '/^d/,/^$/p' your/file | sed -n '3p;'
Post by Brian J. Murrell
[Non-text portions of this message have been removed]
------------------------------------
Loading...