Search code examples
sedinsert

Find a word in a file and then add a line before the next known word


I am trying to add a comment "##INSERT-A NEW-LINE-HERE" in a file which looks like this:

    location /test/ {
            #keepalive_timeout 0;
            proxy_http_version 1.1;
            proxy_set_header Accept-Encoding "";
            proxy_set_header X-Forwarded-For;
            proxy_set_header Access-Control-Al;

            proxy_pass https://test.company.in/;
            proxy_set_header Host test.company.in;
            #proxy_set_header Referer $http_referer;
            
            ##INSERT-A NEW-LINE-HERE
            
            if ($http_referer ~* ^(https?\:\/\/)(.*?)\/test\/(.*?)$) {
                    set $real_referer https://test.company.in/$3;
            }
            proxy_set_header Referer $real_referer;


            proxy_redirect http://test.company.in/ https://sample.company.in/test/;
            proxy_redirect https://test.company.in/ https://sample.company.in/test/;
            proxy_redirect / https://sample.company.in/test/
            sub_filter_types *;
            sub_filter 'href="/' 'href="/test/';
            sub_filter 'href="../' 'href="../test/';
            sub_filter 'href=../' 'href=../test/';

    }
 location /test_app1/ {
            #keepalive_timeout 0;
            proxy_http_version 1.1;
            proxy_set_header Accept-Encoding "";
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Access-Control-Allow-Origin https://test.app1.in;

            proxy_pass https://test.app1.in/;
            proxy_set_header Host test.app1.in;
            #proxy_set_header Referer $http_referer;
            
            ##INSERT-A NEW-LINE-HERE
            
            if ($http_referer ~* ^(https?\:\/\/)(.*?)\/test\/(.*?)$) {
                    set $real_referer https://test.app1.in/$3;
            }
            proxy_set_header Referer $real_referer;

            proxy_redirect http://test.app1.in/ https://sample.company.in/test_app1/;
            proxy_redirect http://sample.company.in/ https://sample.company.in/test_app1/;
            proxy_redirect https://test.app1.in/ https://sample.company.in/test_app1/;
            proxy_redirect https://test.app1.in https://sample.company.in/test_app1/;
            proxy_redirect https://sample.company.in/ https://sample.company.in/test_app1/;
            
            ##DO-NOTINSERT-A NEW-LINE-HERE-AS-IT-IS-SECOND-IF-BLOCK
            
            if ($http_referer ~* ^(https?\:\/\/)(.*?)\/test\/(.*?)$) {
                    set $real_referer https://test.app1.in/$3;
            }
            proxy_set_header Referer $real_referer;

            proxy_redirect / https://sample.company.in/test_app1/;



    }

I want to add a comment ONLY before first IF-BLOCK after every LOCATION-BLOCK

Here is what I have currently:

sed -i '/if ($http_referer/i
##INSERT-A NEW-LINE-HERE
' file.conf

Using this above code the comment appears above all IF BLOCKS and I only need this comment for first IF-BLOCK after every LOCATION-BLOCK


Solution

  • This might work for you (GNU sed):

    sed '/^\s*location .*{$/{:a;n;/^\s*if /!ba;h;s/\S.*/INSERTED LINE\n/;G}' file
    

    Match on location and then continue reading lines until another match of one beginning if.

    Make a copy of if, replace the line (starting at the first non white spaced character) with the intended text (with a further newline).

    Append the original line.

    Repeat.

    N.B. This assumes all such locations contain an if clause somewhere.