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

  • Home
  • SEARCH
  • 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 7932273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:57:55+00:00 2026-06-03T20:57:55+00:00

I want to replace a single line in a file with multiple lines, e.g.,

  • 0

I want to replace a single line in a file with multiple lines, e.g., I want to replace a particular function call, say,

foo(1,2)

with

if (a > 1) {  
    foo(1,2)  
} else {  
    bar(1,2)  
}

How can I do it in bash?

  • 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-03T20:57:57+00:00Added an answer on June 3, 2026 at 8:57 pm

    This is what the sed s command was built for:

    shopt -s extglob
    
    ORIG="foo(1,2)"
    REP="if (a > 1) {  
          foo(1,2)  
    } else {  
          bar(1,2)  
    }"
    
    REP="${REP//+(
    )/\\n}"
    
    sed "s/$ORIG/$REP/g" inputfile > outputfile
    

    Note that the REP="${REP//\+( )/\\n}" lines are only needed if you want to define the REP in the formatted way that I did on line two. It might be simpler if you just used \n and \t in REP to begin with.

    Edit: Note! You need to escape ' and \ as well in your REP if you have them.

    Edit in response to the OP’s question

    To change your original file without creating a new file, use sed’s --in-place flag, like so:

    sed --in-place "s/$ORIG/$REP/g" inputfile
    

    Please be careful with the --in-place flag. Make backups before you run it because all changes will be permanent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I want to replace text inside the single quotes in line $key =
I need to replace two patterns in each line of a file. Lets say
I want to find/replace a character/pattern ONLY inside SQL comments ( single line comments
How to replace every single characters, till the end of a line, using Java
I want to remove all single line comments (eg //comments ) from my code
I am extracting a file using $data = file_get_contents(file.html); I want to replace all
Say I have this file: cat > test.txt <<EOF line one word line two
I want to replace two things in one select statement. Now I can replace
How can I trim multiple line breaks? for instance, $text =similique sunt in culpa
I want to replace all pairs of square brackets in a file, e.g., [some

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.