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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:43:14+00:00 2026-05-31T05:43:14+00:00

I know how to do a search and replace amongst group of files: perl

  • 0

I know how to do a search and replace amongst group of files:

perl -pi -w -e ‘s/search/replace/g;’ *.php

So I can use that to search for a keyword or phrase and change it. But I have a more complicated task I dont know how to do.

I want to do a search and replace among all my php files to search for a specific Keyword and replace it with the File Name minus the extension.

Example: Search the file Mountains.php for the keyword Trees and everywhere you see Trees, replace it with Mountains

Of course I want to be able to do that in batch, for a few hundred php files all with different names, however, all containing the search term Trees.

If someone is looking for an extra challenge, haha, it would be even better if I could do a more complex scenario such as….

Example: Search the file MountainTowns.php for the keyword Trees and everywhere you see Trees, replace it with “Mountain Towns” (note the extra space, Capital Letters could would indicate where spaces go)

Thanks for your time and considering my question.

  • 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-31T05:43:16+00:00Added an answer on May 31, 2026 at 5:43 am

    Well, the filename is in $ARGV, so there is not much more work needed.

    perl -i -pe '($x=$ARGV)=~s{.php$}{};s{Trees}{$x}g' BlueMountains.php RedMountains.php
    

    Add in

    $x=~s{(.)([A-Z])}{$1 $2}g;
    

    to add the space before upcased letters, for a complete line of

    perl -i -pe '($x=$ARGV)=~s{.php$}{};$x=~s{(.)([A-Z])}{$1 $2}g;s{Trees}{$x}g' BlueRedMountains.php
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a little search application (I know I can use match against). But
As far as I know in perl global '/g' flag means that search will
I have a php file that can read the contents of other files perfectly
sorry for this question, but i dont know where to search for it, or
Pretty sure this has been asked already, but I don't know what to search
Anyone know what the best way is to search on a Field that hold
How can I search first in model (i know how to do this). And
I know that with Vim's substitution command you can specific a range of lines:
I am trying to do a dynamic search and replace with Perl on the
I've hit a wall. Does anybody know a good text editor that has search

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.