Para responder à sua situação geral, construímos um código ed
apropriado com antecedência, com base no tipo de dados fornecidos.
re=5 n=2 m=3
code=$(
prev="/$re/-$m,/$re/-1d"
next="/$re/+1,/$re/+${n}d"
case "$m/$n" in
0/0) set -- ;;
0/?*) set -- "$next" "w" ;;
?*/0) set -- "$prev" "w" ;;
*) set -- "$prev" "$next" "w" ;;
esac
printf '%s\n' ${1+"$@"} "q"
)
ed -s filename - <<eof
$code
eof
Uma maneira pode ser: isso usa o editor ed
para realizar o endereçamento relativo, pois é nisso que o seu problema está centrado.
n=3 m=2 re=5
ed -s filename - <<eof
/$re/-$m,/$re/-1d
.+1,.+${n}d
wq
eof
Explicação:
1. Line 3 after var substitution becomes
/5/-2,/5/-1
What it does is, sitting on line which satisfies the regex /5/, it looks 2 lines behind and stops looking 1 line before the /5/ line or the current line and deletes that bunch. Remember the current line is not deleted.
2. Line 4, after var sub becomes
.+1,.+3d
. is the nickname for the current line, which in our case is /5/
So, starting fron one line after the current upto 3 lines after the current, delete them all. Note the current line is still untouched.
3. Line 5 is wq which means save the modified file back onto itself and quit.
For more on info google the manual for gnu ed editor.