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 6742945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:54:10+00:00 2026-05-26T11:54:10+00:00

The problem I have is pretty straightforward (or so it seems). All I want

  • 0

The problem I have is pretty straightforward (or so it seems). All I want to do is replace a paragraph of text (it’s a header comment) with another paragraph. This will need to happen across a diverse number of files in a directory hierarchy (source code tree).

The paragraph to be replaced must be matched in it’s entirety as there are similar text blocks in existence.

e.g.

To Replace

// ----------
// header
// comment
// to be replaced
// ----------

With

// **********
// some replacement
// text
// that could have any
// format
// **********

I have looked at using sed and from what I can tell the most number of lines that it can work on is 2 (with the N command).

My question is: what is the way to do this from the linux command line?

EDIT:

Solution obtained: Best solution was Ikegami’s, fully command line and best fit for what I wanted to do.

My final solution required some tweaking; the input data contained a lot of special characters as did the replace data. To deal with this the data needs to be pre processed to insert appropriate \n’s and escape characters. The end product is a shell script that takes 3 arguments; File containing text to search for, File containing text to replace with and a folder to recursively parse for files with .cc and .h extension. It’s fairly easy to customise from here.

SCRIPT:

#!/bin/bash
if [ -z $1 ]; then
    echo 'First parameter is a path to a file that contains the excerpt to be replaced, this must be supplied'
  exit 1
fi

if [ -z $2 ]; then
    echo 'Second parameter is a path to a file contaiing the text to replace with, this must be supplied'
  exit 1
fi

if [ -z $3 ]; then
    echo 'Third parameter is the path to the folder to recursively parse and replace in'
  exit 1
fi

sed 's!\([]()|\*\$\/&[]\)!\\\1!g' $1 > temp.out
sed ':a;N;$!ba;s/\n/\\n/g' temp.out > final.out
searchString=`cat final.out`
sed 's!\([]|\[]\)!\\\1!g' $2 > replace.out
replaceString=`cat replace.out`

find $3 -regex ".*\.\(cc\|h\)" -execdir perl -i -0777pe "s{$searchString}{$replaceString}" {} +
  • 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-05-26T11:54:11+00:00Added an answer on May 26, 2026 at 11:54 am
    find -name '*.pm' -exec perl -i~ -0777pe'
        s{// ----------\n// header\n// comment\n// to be replaced\n// ----------\n}
         {// **********\n// some replacement\n// text\n// that could have any\n// format\n// **********\n};
    ' {} +
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty big problem with my TabHost. Although I have declared all
This is pretty much the same problem i have, except with very different code:
This is a pretty straight-forward problem. I basically have a div block on a
OK, this begins to drive me crazy. I have an asp.net webapp. Pretty straightforward,
I am pretty sure this is a straightforward problem I must be confused about
I have a pretty simple problem. Basically I have an array called $list that
I have a pretty basic problem but I can't seem to figure it out...
My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
I have a problem with WCF. My testing code is pretty simple. I call
Here is my problem. I have created a pretty heavy readonly class making many

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.