2

I am currently playing with sed to get contents between two lines. I got a nice tutorial: http://www.cyberciti.biz/faq/unix-linux-sed-print-only-matching-lines-command/

In this tutorial, I found:

sed -n -e '/regexpA/,/regexpB/p' input.file

The command above will print also the lines matched regexpA and regexpB, but I would want to escape these two line, say these two matched lines would not print to STDOUT, is there any beautiful solution for this?

Thanks in advance.

chicks
  • 2,393
  • 3
  • 24
  • 40
Rui
  • 3,454
  • 6
  • 37
  • 70

3 Answers3

5

How about this:

sed '1,/regexpA/d;/regexpB/,$d' input.file
Beta
  • 96,650
  • 16
  • 149
  • 150
1

sed is for simple substitutions on individual lines, just use awk:

$ cat file
a
b
c
d
e

$ awk '/b/{f=1} f; /d/{f=0}' file
b
c
d

$ awk 'f; /b/{f=1} /d/{f=0}' file
c
d

$ awk '/b/{f=1} /d/{f=0} f' file
b
c

$ awk '/d/{f=0} f; /b/{f=1}' file
c

See https://stackoverflow.com/a/17914105/1745001 for other common awk range searching idioms.

Community
  • 1
  • 1
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

This might work for you (GNU sed):

sed '/regexpA/,/regexpB/!d;//d' file

Delete all lines before and after regexpA and regexpB and then delete lines which match regexpA and regexpB.

potong
  • 55,640
  • 6
  • 51
  • 83