A
A
Alexander Odintsov2015-12-11 13:49:01
linux
Alexander Odintsov, 2015-12-11 13:49:01

How to search and replace multiple lines of text in Linux?

There is a task to cut out the google tag manager code from a huge number of pages. Pages are just php on the server, I wanted to find a quick solution, but so far it has not been possible.
The code itself looks something like this

<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-000000"
                  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
        '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-000000');</script>
<!-- End Google Tag Manager -->

Here is such a regular
<!-- Google Tag(.|\n|\t|\r)* End Google Tag Manager -->
I find it in any editor.
But when I type in the console:
find . -type f -iname "*.php" | xargs perl -i.bak -pe "s/<(.*) Google Tag (.|\n|\r)* End Google Tag Manager -->$//g"

or here's another option
find . -type f -iname "*.php" -exec sed -i "s/<(.*) Google Tag (.|\n|\r)* End Google Tag Manager -->$//g" {} \;

Nothing happens.
I checked - you can cut line by line, but some solution is not correct.
Can anyone come across a similar one, how to find and replace several code terms through the console ??
The solution is found, this is how it works.
find ~~m~test -type f -iname "*.php" -exec sed -i '/<!-- Google Tag Manager -->/,/<!-- End Google Tag Manager -->/d' {} \;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
romy4, 2015-12-11
@1tsov

regex on the command line is a little different. parentheses must be escaped if you want to use them as parentheses.
find. -type f -iname "*.txt" -exec sed -i "s/<\(.*\) Google Tag \(.|\n|\r\)* End Google Tag Manager -->$//g " {} \;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question