2

This questions been asked a number of times but the answers given are ALL GNU sed specific.

sed -i '' "/${FIND}/,+2d" "$FILE"

Gives a "expected context address" error.

Please could someone give me an example of how to use BSD sed to delete X lines including the match and another X lines excluding the match?

Oliver Pearmain
  • 19,885
  • 13
  • 86
  • 90

2 Answers2

2

The problem is that this is simply not a job for sed, it's job for awk.

$ seq 5 | awk '/3/{c=2} !(c&&c--)'
1
2
5

Just set the variable c to however many lines you want to skip.

See also https://stackoverflow.com/a/18409469/1745001.

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

The following sed editing script (run with -n) will emulate what GNU sed /^pattern/,+2d does:

/^pattern/{
    n
    n
    d
}

p

When matching /^pattern/, it will read the next two lines of input, and then discard everything (restarting the cycle). When the pattern doesn't match, the line is printed.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52