Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:47:07+00:00 2026-06-07T03:47:07+00:00

In sed , I’d like to replace a multi-line block of text after a

  • 0

In sed, I’d like to replace a multi-line block of text after a match, for example, after matching “foo”, supposing its line number is 0. I want to replace the text block from line -3 to line +5, i.e. the text bock between the third line proceeding the matching line and the fifth line after the matching line, by another text block bar1\nbar2. I’d like to be able to do this in two scenarios:

1) Keep the matching line after the replaced block;
2) remove the matching line together with those lines -3 and +5.

Please help me.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:47:08+00:00Added an answer on June 7, 2026 at 3:47 am

    This might work (GNU sed):

    seq 31|sed 's/5/& match/' >/tmp/file
    sed ':a;$q;N;s/\n/&/3;Ta;/match/!{P;D};:b;$bc;N;s/\n/&/8;Tb;:c;s/.*/bar1\nbar2/' /tmp/file
    1
    bar1
    bar2
    11
    bar1
    bar2
    21
    bar1
    bar2
    31
    sed ':a;$q;N;s/\n/&/3;Ta;/match/!{P;D};h;s/\([^\n]*\n\)*\([^\n]*match[^\n]*\).*/\2/;x;:b;$bc;N;s/\n/&/8;Tb;:c;s/.*/bar1\nbar2/;G' /tmp/file
    1
    bar1
    bar2
    5 match
    11
    bar1
    bar2
    15 match
    21
    bar1
    bar2
    25 match
    31
    

    Explanation:

    The commands fall into two halves:

    1. The first half keeps a moving window of 3 lines.
    2. Following a match 5 further lines are appended.

    The details are as follows:

    • :a is a loop placeholder
    • $q on end-of-file print all lines within the pattern space (PS).
    • N append the next line to the PS
    • s/\n/&/3 replace the 3rd newline character by itself. This a counting device for checking that 3 lines are in the PS.
    • Ta if the previous substitution failed loop to the loop placeholder a
    • /match/!{P;D} look the match and if it fails print upto the first newline and then delete that line and it’s newline (this invokes a new cycle).
    • :b is a loop placeholder N.B. a match has been found at this point.
    • $bc if end-of-file branch forward to the placeholder c
    • N append the next line to the PS
    • s/\n/&/8 replace the 8th (3 before 5 after) newline character by itself. This a counting device for checking that 5 lines are appended to the PS
    • Tb if the previous substitution failed loop to the loop placeholder b
    • :c is a loop placeholder
    • s/.*/bar1\nbar2/ replace the PS with the required string.

    The second one liner makes a copy of the match line and appends it to the substituted string.

    Alternative solutions:

    sed -r ':a;$!N;s/[^\n]*/&/9;$!Ta;/^([^\n]*\n){3}([^\n]*match[^\n]*)\n.*/!{P;D};c\bar1\nbar2' file
    
    sed -r ':a;$!N;s/[^\n]+/&/9;$!Ta;/^([^\n]*\n){3}([^\n]*match[^\n]*)\n.*/!{P;D};s//\bar1\nbar2\n\2/' file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to use sed to insert a line after [Block B] in the
I am using sed -e s/foo/$bar/ -e s/some/$text/ file.whatever to replace a phrase in
Using bash/sed, I am trying to search for matching string and when a match
Using sed, if there is a space character on a line in a file,
I used the following sed command to find and replace an old string with
sed 's/$/<br>/' mytext.txt worked but output it all on the command line. I want
I was testing the sed utility with a txt file with a single line
I am new to sed. I have a text file and I want to
New to sed and could use some help. I would like to turn this
I would like one sed command to accomplish the following: $ sed s'/:/ /g'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.