I want to use sed to give me some lines from a file. I want to extract these lines only within some part of the file, which I know. E.g. I have a file with 100 lines, and I am seeking for lines withing line 10 and 30 with a specific pattern.

My current solution is to use two sed commands: The first prints every thing between the line numbers I have, e.g.

sed -n "10,30p" target_file.file

The second searches withing that result with a pattern

sed -n '/pattern/p'

So in the end I have this:

sed -n "10,30p" target_file.file | sed -n '/pattern/p'

Is there a way, to make one command out of two ?

1 Answer 1


You can:

$ sed -n '10,30{
}' <file

That's standard syntax for now. You can also use:

$ sed -n '10,30{/pattern/p;}' <file

in next POSIX version.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .