Skip to main content
edited body
Source Link
user232326
user232326

A sed (but only GNU sed) solution without loops and using simple ranges is:

sed '/^two$/,+5{/^two$/,+4{b};s/.*/& hello/}' 

The only thing to adjust is the match regexps and the +3+5 and +2+4 counters.

For 100 lines after the match:

sed '/two/,+100{/two/,+99{b};s/.*/& Modified/}' 

Maybe this could clarify how it works:

$ seq 10 | sed '/4/,+3{/4/,+2{s/.*/&changed/;b};s/.*/& Modified.../}' 1 2 3 4changed 5changed 6changed 7 Modified... 8 9 10 

A sed (but only GNU sed) solution without loops and using simple ranges is:

sed '/^two$/,+5{/^two$/,+4{b};s/.*/& hello/}' 

The only thing to adjust is the match regexps and the +3 and +2 counters.

For 100 lines after the match:

sed '/two/,+100{/two/,+99{b};s/.*/& Modified/}' 

Maybe this could clarify how it works:

$ seq 10 | sed '/4/,+3{/4/,+2{s/.*/&changed/;b};s/.*/& Modified.../}' 1 2 3 4changed 5changed 6changed 7 Modified... 8 9 10 

A sed (but only GNU sed) solution without loops and using simple ranges is:

sed '/^two$/,+5{/^two$/,+4{b};s/.*/& hello/}' 

The only thing to adjust is the match regexps and the +5 and +4 counters.

For 100 lines after the match:

sed '/two/,+100{/two/,+99{b};s/.*/& Modified/}' 

Maybe this could clarify how it works:

$ seq 10 | sed '/4/,+3{/4/,+2{s/.*/&changed/;b};s/.*/& Modified.../}' 1 2 3 4changed 5changed 6changed 7 Modified... 8 9 10 
added 19 characters in body
Source Link
user232326
user232326

A sed (but only GNU sed) solution without loops and using simple ranges is:

sed '/two^two$/,+5{/two^two$/,+4{b};s/.*/&hello& hello/}' 

The only thing to adjust is the match regexps and the +3 and +2 counters.

For 100 lines after the match:

sed '/two/,+100{/two/,+99{b};s/.*/& Modified/}' 

Maybe this could clarify how it works:

$ seq 10 | sed '/4/,+3{/4/,+2{s/.*/&changed/;b};s/.*/&hello& Modified.../}' 1 2 3 4changed 5changed 6changed 7hello7 Modified... 8 9 10 

A sed (but only GNU sed) solution without loops and using simple ranges is:

sed '/two/,+5{/two/,+4{b};s/.*/&hello/}' 

The only thing to adjust is the match regexps and the +3 and +2 counters.

For 100 lines after the match:

sed '/two/,+100{/two/,+99{b};s/.*/& Modified/}' 

Maybe this could clarify how it works:

$ seq 10 | sed '/4/,+3{/4/,+2{s/.*/&changed/;b};s/.*/&hello/}' 1 2 3 4changed 5changed 6changed 7hello 8 9 10 

A sed (but only GNU sed) solution without loops and using simple ranges is:

sed '/^two$/,+5{/^two$/,+4{b};s/.*/& hello/}' 

The only thing to adjust is the match regexps and the +3 and +2 counters.

For 100 lines after the match:

sed '/two/,+100{/two/,+99{b};s/.*/& Modified/}' 

Maybe this could clarify how it works:

$ seq 10 | sed '/4/,+3{/4/,+2{s/.*/&changed/;b};s/.*/& Modified.../}' 1 2 3 4changed 5changed 6changed 7 Modified... 8 9 10 
Source Link
user232326
user232326

A sed (but only GNU sed) solution without loops and using simple ranges is:

sed '/two/,+5{/two/,+4{b};s/.*/&hello/}' 

The only thing to adjust is the match regexps and the +3 and +2 counters.

For 100 lines after the match:

sed '/two/,+100{/two/,+99{b};s/.*/& Modified/}' 

Maybe this could clarify how it works:

$ seq 10 | sed '/4/,+3{/4/,+2{s/.*/&changed/;b};s/.*/&hello/}' 1 2 3 4changed 5changed 6changed 7hello 8 9 10