Script to add a block in a few hundred files

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















I want to add the following lines



if ($http_user_agent ~* "somewebsite" ) 
return 444;



to all my vhosts in NGINX as follows



server 
location /
want_to_add_those_3_lines_here




Any ideas?










share|improve this question
























  • Use an include statement.

    – Richard Smith
    Feb 6 at 13:58















2















I want to add the following lines



if ($http_user_agent ~* "somewebsite" ) 
return 444;



to all my vhosts in NGINX as follows



server 
location /
want_to_add_those_3_lines_here




Any ideas?










share|improve this question
























  • Use an include statement.

    – Richard Smith
    Feb 6 at 13:58













2












2








2








I want to add the following lines



if ($http_user_agent ~* "somewebsite" ) 
return 444;



to all my vhosts in NGINX as follows



server 
location /
want_to_add_those_3_lines_here




Any ideas?










share|improve this question
















I want to add the following lines



if ($http_user_agent ~* "somewebsite" ) 
return 444;



to all my vhosts in NGINX as follows



server 
location /
want_to_add_those_3_lines_here




Any ideas?







linux text-processing sed nginx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 7 at 15:26









Jürgen

192110




192110










asked Feb 6 at 11:30









JodiL79JodiL79

111




111












  • Use an include statement.

    – Richard Smith
    Feb 6 at 13:58

















  • Use an include statement.

    – Richard Smith
    Feb 6 at 13:58
















Use an include statement.

– Richard Smith
Feb 6 at 13:58





Use an include statement.

– Richard Smith
Feb 6 at 13:58










1 Answer
1






active

oldest

votes


















0














Using GNU sed



This task can be can be done with sed.



However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.



The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.



But the most helpful tool in this context is the -z option that GNU sed offers and that we take great benefit of below.



You can tell whether your version of sed is a GNU implementation by running



sed --version


Turning a multi-line into a single-line problem



With the -z option that GNU sed offers, you have a choice of what sed thinks is a line.



The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z option, sed considers lines to be NULL byte terminated instead.



It is quite likely that sed -z will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.



Solution



Let's chose sed-script as the name of the following script:



#!/bin/bash

pattern="
(
server n
location / n
)(
n
)
"

replacement="
if ($http_user_agent ~* "somewebsite" ) n
return 444;n
n
"

sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"


If you make this script executable and call it like this:



./sed-script INFILE OUTFILE


it will process INFILE looking for every occurrence of



server 
location /




replaces it by



server 
location /
if ($http_user_agent ~* "somewebsite" )
return 444;





and puts the result into OUTFILE.



The only reason why we use a script here is to keep the structure of the sed command clear.



Since the search pattern contains a slash (/), an exclamation mark (!) is used to delimit the fields of the sed s command.






share|improve this answer
























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499023%2fscript-to-add-a-block-in-a-few-hundred-files%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Using GNU sed



    This task can be can be done with sed.



    However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.



    The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.



    But the most helpful tool in this context is the -z option that GNU sed offers and that we take great benefit of below.



    You can tell whether your version of sed is a GNU implementation by running



    sed --version


    Turning a multi-line into a single-line problem



    With the -z option that GNU sed offers, you have a choice of what sed thinks is a line.



    The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z option, sed considers lines to be NULL byte terminated instead.



    It is quite likely that sed -z will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.



    Solution



    Let's chose sed-script as the name of the following script:



    #!/bin/bash

    pattern="
    (
    server n
    location / n
    )(
    n
    )
    "

    replacement="
    if ($http_user_agent ~* "somewebsite" ) n
    return 444;n
    n
    "

    sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"


    If you make this script executable and call it like this:



    ./sed-script INFILE OUTFILE


    it will process INFILE looking for every occurrence of



    server 
    location /




    replaces it by



    server 
    location /
    if ($http_user_agent ~* "somewebsite" )
    return 444;





    and puts the result into OUTFILE.



    The only reason why we use a script here is to keep the structure of the sed command clear.



    Since the search pattern contains a slash (/), an exclamation mark (!) is used to delimit the fields of the sed s command.






    share|improve this answer





























      0














      Using GNU sed



      This task can be can be done with sed.



      However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.



      The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.



      But the most helpful tool in this context is the -z option that GNU sed offers and that we take great benefit of below.



      You can tell whether your version of sed is a GNU implementation by running



      sed --version


      Turning a multi-line into a single-line problem



      With the -z option that GNU sed offers, you have a choice of what sed thinks is a line.



      The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z option, sed considers lines to be NULL byte terminated instead.



      It is quite likely that sed -z will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.



      Solution



      Let's chose sed-script as the name of the following script:



      #!/bin/bash

      pattern="
      (
      server n
      location / n
      )(
      n
      )
      "

      replacement="
      if ($http_user_agent ~* "somewebsite" ) n
      return 444;n
      n
      "

      sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"


      If you make this script executable and call it like this:



      ./sed-script INFILE OUTFILE


      it will process INFILE looking for every occurrence of



      server 
      location /




      replaces it by



      server 
      location /
      if ($http_user_agent ~* "somewebsite" )
      return 444;





      and puts the result into OUTFILE.



      The only reason why we use a script here is to keep the structure of the sed command clear.



      Since the search pattern contains a slash (/), an exclamation mark (!) is used to delimit the fields of the sed s command.






      share|improve this answer



























        0












        0








        0







        Using GNU sed



        This task can be can be done with sed.



        However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.



        The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.



        But the most helpful tool in this context is the -z option that GNU sed offers and that we take great benefit of below.



        You can tell whether your version of sed is a GNU implementation by running



        sed --version


        Turning a multi-line into a single-line problem



        With the -z option that GNU sed offers, you have a choice of what sed thinks is a line.



        The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z option, sed considers lines to be NULL byte terminated instead.



        It is quite likely that sed -z will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.



        Solution



        Let's chose sed-script as the name of the following script:



        #!/bin/bash

        pattern="
        (
        server n
        location / n
        )(
        n
        )
        "

        replacement="
        if ($http_user_agent ~* "somewebsite" ) n
        return 444;n
        n
        "

        sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"


        If you make this script executable and call it like this:



        ./sed-script INFILE OUTFILE


        it will process INFILE looking for every occurrence of



        server 
        location /




        replaces it by



        server 
        location /
        if ($http_user_agent ~* "somewebsite" )
        return 444;





        and puts the result into OUTFILE.



        The only reason why we use a script here is to keep the structure of the sed command clear.



        Since the search pattern contains a slash (/), an exclamation mark (!) is used to delimit the fields of the sed s command.






        share|improve this answer















        Using GNU sed



        This task can be can be done with sed.



        However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.



        The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.



        But the most helpful tool in this context is the -z option that GNU sed offers and that we take great benefit of below.



        You can tell whether your version of sed is a GNU implementation by running



        sed --version


        Turning a multi-line into a single-line problem



        With the -z option that GNU sed offers, you have a choice of what sed thinks is a line.



        The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z option, sed considers lines to be NULL byte terminated instead.



        It is quite likely that sed -z will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.



        Solution



        Let's chose sed-script as the name of the following script:



        #!/bin/bash

        pattern="
        (
        server n
        location / n
        )(
        n
        )
        "

        replacement="
        if ($http_user_agent ~* "somewebsite" ) n
        return 444;n
        n
        "

        sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"


        If you make this script executable and call it like this:



        ./sed-script INFILE OUTFILE


        it will process INFILE looking for every occurrence of



        server 
        location /




        replaces it by



        server 
        location /
        if ($http_user_agent ~* "somewebsite" )
        return 444;





        and puts the result into OUTFILE.



        The only reason why we use a script here is to keep the structure of the sed command clear.



        Since the search pattern contains a slash (/), an exclamation mark (!) is used to delimit the fields of the sed s command.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 7 at 14:32

























        answered Feb 7 at 0:23









        JürgenJürgen

        192110




        192110



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499023%2fscript-to-add-a-block-in-a-few-hundred-files%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay