Skip to content
Advertisement

sed pattern matching between lines

I am trying to write a sed script to only output the lines of a file if the line has the /pattern/ and is between line x and line y. I have the following:

JavaScript

select.sed:

JavaScript

If my text.file is the the following:

JavaScript

The desired output would be

JavaScript

How would I set a range for lines 2-4 and only print values with “pattern”?

Advertisement

Answer

Try:

JavaScript
  • -n means do not print pattern space automatically.
  • x,y means operate only on lines between x. and y. line.
  • /regexp/p means print pattern space if regexp matches against pattern space.
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement