?
Solved

global search and replace?

Posted on 2003-02-21
3
Medium Priority
?
151 Views
Last Modified: 2013-11-18
I need to go through every file in my webserver root directory and its subdirectories (over 600 files) and replace this text:

<!--#include virtual="/header/header.html"-->

with this text:

<!--#include virtual="/cgi/header/header.cgi?sec=header"-->

Can someone give me a good idea how to do it? I am on Redhat 8.0

Thanks!

0
Comment
Question by:rustycp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
jdfox earned 400 total points
ID: 7996202
How about this script:

#!/bin/bash
mkdir temp
for n in `ls`;
do sed s/"original string"/"new string"/g $n > temp/$n
done

Perl could probably do it in two short lines, but hey. :-)

If you had more than just HTML files in that directory, you could change that script like this, so it would only work on .html files:

#!/bin/bash
mkdir temp
for n in `find . -name "*.html"`;
do sed s/"original string"/"new string"/g $n > temp/$n
done


HTH,
--
JF
0
 
LVL 2

Author Comment

by:rustycp
ID: 8000216
cool.  I could do it in Perl, but I really wondered about a shell script possiblity. Thanks!
0
 
LVL 1

Expert Comment

by:arn0ld
ID: 8005366
The solution is "safe" in that it leaves the original
files unchanged. However, it will change all *html files
even if they do not contain "original string". I like to retain "last change date"
on my files and would use something like the following.

cd root_dir
for n in $(find . -type f -name "*.html" -exec grep -l "original string" {} \;)
do
ex $n<<!
g/original string/s//new string/
w
q
!
done

I would backup my original files before I ran this.
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question