How do I perform a multi-file, multi-line, multi-directory find/replace in linux?

Posted on 2010-01-12
Medium Priority
Last Modified: 2013-12-15
While I've looked at:




when I try to use them, neither accomplishes what I am looking for.  basically I want to do site-wide replace of a multiple line javascript by using a command line find/replace in linux.

in terms of modifying perl -pi -w -e 's/\.\.\/includes\/style\.css/admin\.css/g;' *.php

I am not sure how to change it to handle (a) multiple lines and (b) multiple directories.

here's what I want to find:

<!-- Start Quantcast tag -->
<script type="text/javascript">
if(window.location.protocol != 'https:') {
    document.write('<script src="http://edge.quantserve.com/quant.js" type="text/javascript"><\/script>');
    document.write('<script type="text/javascript">_qacct="p-XXXXXXXXXXXXXXXX-";quantserve();<\/script>');
<!-- End Quantcast tag -->

and replace it with:

<script type="text/javascript">
<script type="text/javascript" src="//secure.quantserve.com/quant.js"></script>
<img src="//secure.quantserve.com/pixel/p-XXXXXXXXXXX-.gif" style="display: none;" border="0" height="1" width="1" alt="Quantcast"/>

any suggestions?
Question by:virmaior
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
  • 2
  • 2
LVL 51

Accepted Solution

ahoffmann earned 2000 total points
ID: 26311877
assuming you have a perl script which makes your changes for one file:

find /path/to/dir/where/to/start/ -type f -name \*.php -exec your-script.pl -pi {} \;

# to be improved in many ways ...
LVL 20

Author Comment

ID: 26315275
definitely a step in the right direction...

I don't have the multi-line regex as of yet, so i'm going to leave it open for a day or two to see if anyone appears with one.

if not, i'll give you the points and ask that in the perl area.
LVL 51

Assisted Solution

ahoffmann earned 2000 total points
ID: 26321055
for multi-line regex see m operator
  man perlre
LVL 20

Author Comment

ID: 26325849

I think these are the pieces i need.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

801 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