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,050
  • 2
  • 20
  • 34
Rui
  • 3,192
  • 5
  • 34
  • 59

3 Answers3

5

How about this:

sed '1,/regexpA/d;/regexpB/,$d' input.file
Beta
  • 92,251
  • 12
  • 140
  • 145
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
  • 172,331
  • 17
  • 70
  • 167
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
  • 51,370
  • 6
  • 49
  • 80