Insert multiline text before the first matching line in a file

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












we want to add the following lines in log4j file



log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
log4j.appender.DRFA.MaxBackupIndex=100
log4j.appender.DRFA.MaxFileSize=10MB


but these lines must be before the first line that include the word - DatePattern , and no matter if line is with mark or not



#log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


or



log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


note - in case DatePattern word apears couple times in file , then the three lines must be set only before the first line that include - DatePattern



example 1 ( expected output )



log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
log4j.appender.DRFA.MaxBackupIndex=100
log4j.appender.DRFA.MaxFileSize=10MB


#log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


example 2 ( expected output )



log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
log4j.appender.DRFA.MaxBackupIndex=100
log4j.appender.DRFA.MaxFileSize=10MB


log4j.appender.DRFA.DatePattern=.yyyy-MM-dd






share|improve this question



























    up vote
    3
    down vote

    favorite












    we want to add the following lines in log4j file



    log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
    log4j.appender.DRFA.MaxBackupIndex=100
    log4j.appender.DRFA.MaxFileSize=10MB


    but these lines must be before the first line that include the word - DatePattern , and no matter if line is with mark or not



    #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


    or



    log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


    note - in case DatePattern word apears couple times in file , then the three lines must be set only before the first line that include - DatePattern



    example 1 ( expected output )



    log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
    log4j.appender.DRFA.MaxBackupIndex=100
    log4j.appender.DRFA.MaxFileSize=10MB


    #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


    example 2 ( expected output )



    log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
    log4j.appender.DRFA.MaxBackupIndex=100
    log4j.appender.DRFA.MaxFileSize=10MB


    log4j.appender.DRFA.DatePattern=.yyyy-MM-dd






    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      we want to add the following lines in log4j file



      log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
      log4j.appender.DRFA.MaxBackupIndex=100
      log4j.appender.DRFA.MaxFileSize=10MB


      but these lines must be before the first line that include the word - DatePattern , and no matter if line is with mark or not



      #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


      or



      log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


      note - in case DatePattern word apears couple times in file , then the three lines must be set only before the first line that include - DatePattern



      example 1 ( expected output )



      log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
      log4j.appender.DRFA.MaxBackupIndex=100
      log4j.appender.DRFA.MaxFileSize=10MB


      #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


      example 2 ( expected output )



      log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
      log4j.appender.DRFA.MaxBackupIndex=100
      log4j.appender.DRFA.MaxFileSize=10MB


      log4j.appender.DRFA.DatePattern=.yyyy-MM-dd






      share|improve this question













      we want to add the following lines in log4j file



      log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
      log4j.appender.DRFA.MaxBackupIndex=100
      log4j.appender.DRFA.MaxFileSize=10MB


      but these lines must be before the first line that include the word - DatePattern , and no matter if line is with mark or not



      #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


      or



      log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


      note - in case DatePattern word apears couple times in file , then the three lines must be set only before the first line that include - DatePattern



      example 1 ( expected output )



      log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
      log4j.appender.DRFA.MaxBackupIndex=100
      log4j.appender.DRFA.MaxFileSize=10MB


      #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


      example 2 ( expected output )



      log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
      log4j.appender.DRFA.MaxBackupIndex=100
      log4j.appender.DRFA.MaxFileSize=10MB


      log4j.appender.DRFA.DatePattern=.yyyy-MM-dd








      share|improve this question












      share|improve this question




      share|improve this question








      edited 7 hours ago









      don_crissti

      46k15119151




      46k15119151









      asked 8 hours ago









      yael

      1,787940




      1,787940




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          If I understand your question correctly you merely want to replace this line:



          log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
          -or-
          #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


          With these lines:



          log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
          log4j.appender.DRFA.MaxBackupIndex=100
          log4j.appender.DRFA.MaxFileSize=10MB
          log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
          -or-
          #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


          This GNU sed command can do this:



          $ sed -i 's/(.*appender.DRFA.DatePattern=.*)/log4j.appender.DRFA=org.apache.log4j.RollingFileAppendernlog4j.appender.DRFA.MaxBackupIndex=100nlog4j.appender.DRFA.MaxFileSize=10MBnnn1/' log4j.properties


          With respect to the duplicating of lines. You're not going to have this appender defined more than 1 time:



          appender.DRFA.DatePattern


          Therefore we can look more explicitly for this occurrence, rather than the DatePattern line that you're looking for.



          Example run



          Here's a sample file that has just this line in it:



          $ cat log4j.properties
          #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


          Run the sed command above against this file:



          $ cat log4j.properties
          log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
          log4j.appender.DRFA.MaxBackupIndex=100
          log4j.appender.DRFA.MaxFileSize=10MB
          #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd





          share|improve this answer



















          • 1




            @yael - I updated the answer so that it includes them now.
            – slm♦
            7 hours ago


















          up vote
          5
          down vote













          Using awk:



          awk 'done != 1 && /DatePattern/ 
          print "log4j.appender.DRFA=org.apache.log4j.RollingFileAppender"
          print "log4j.appender.DRFA.MaxBackupIndex=100"
          print "log4j.appender.DRFA.MaxFileSize=10MB"
          done = 1
          1' file >newfile && mv newfile file


          This would print the three lines when the first match of DatePattern occurs in the file. The flag done is then set to 1 which stops the lines from being printed again. The trailing 1 at the very end causes every line in the in-data to be printed.



          If you want empty lines in the output after the three lines, add nn to the end of the last string.



          The output is written to newfile and if awk did not encounter any strange errors, the original is then replaced by this once the awk process terminates.






          share|improve this answer























          • good solution , but I prefer to update the file itself like "sed -i" and not copy the file to the source file for update , is it possible with awk ?
            – yael
            8 hours ago










          • @yael A standard redirection and mv. See updated answer. Not even GNU awk has an in-place editing option. It's not something that awk program usually do.
            – Kusalananda
            8 hours ago







          • 1




            GNU awk has it...
            – don_crissti
            7 hours ago










          • @don_crissti :-) That's an extension, even to GNU awk.
            – Kusalananda
            7 hours ago






          • 1




            Sure but that doesn't change the fact that it exists... Anyway, +1 for answering the actual question here...
            – don_crissti
            7 hours ago

















          up vote
          3
          down vote













          Using GNU sed you may approach it this way also:



          sed -i -e '
          /DatePattern/!b

          i
          log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
          log4j.appender.DRFA.MaxBackupIndex=100
          log4j.appender.DRFA.MaxFileSize=10MB

          :a;n;$!ba
          ' input.file


          And if you are sure that the DatePattern cannot be on the last line of the file, then you could do this also:



          sed -i -e '
          /DatePattern/!b
          r file2add.txt
          N;:a;n;$!ba
          ' input.file


          where you put all the lines to be added into a file, say file2add.txt. Note: with this method, you don't put trailing backslashes in the file file2add.txt.






          share|improve this answer





















          • Yes, those are both proper ways to do it with sed. Future visitors should keep in mind that i requires any embedded backslashes to be escaped.
            – don_crissti
            7 hours ago

















          up vote
          2
          down vote













          Since you tagged this query with perl also, so here goes one obvious way:



          perl -pi -e '
          print <<EOF if /DatePattern/ && !$seen++;
          log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
          log4j.appender.DRFA.MaxBackupIndex=100
          log4j.appender.DRFA.MaxFileSize=10MB
          EOF
          ' input.file


          Note: The added text mustn't contain the standalone string EOF on a line by itself. The backslashing of the heredoc is necessary , lest any variables get expanded .



          If you cannot be sure of the above constraints in the text to add, then do this:



          perl -pi -e '
          next if !/DatePattern/ || $seen++;
          open my $fh, "<", "file2add.txt" or die "Could not open file for reading:$!n";
          $_ = join "", <$fh>, $_;
          ' input.file





          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f460684%2finsert-multiline-text-before-the-first-matching-line-in-a-file%23new-answer', 'question_page');

            );

            Post as a guest






























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            If I understand your question correctly you merely want to replace this line:



            log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
            -or-
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            With these lines:



            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
            -or-
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            This GNU sed command can do this:



            $ sed -i 's/(.*appender.DRFA.DatePattern=.*)/log4j.appender.DRFA=org.apache.log4j.RollingFileAppendernlog4j.appender.DRFA.MaxBackupIndex=100nlog4j.appender.DRFA.MaxFileSize=10MBnnn1/' log4j.properties


            With respect to the duplicating of lines. You're not going to have this appender defined more than 1 time:



            appender.DRFA.DatePattern


            Therefore we can look more explicitly for this occurrence, rather than the DatePattern line that you're looking for.



            Example run



            Here's a sample file that has just this line in it:



            $ cat log4j.properties
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            Run the sed command above against this file:



            $ cat log4j.properties
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd





            share|improve this answer



















            • 1




              @yael - I updated the answer so that it includes them now.
              – slm♦
              7 hours ago















            up vote
            2
            down vote



            accepted










            If I understand your question correctly you merely want to replace this line:



            log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
            -or-
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            With these lines:



            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
            -or-
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            This GNU sed command can do this:



            $ sed -i 's/(.*appender.DRFA.DatePattern=.*)/log4j.appender.DRFA=org.apache.log4j.RollingFileAppendernlog4j.appender.DRFA.MaxBackupIndex=100nlog4j.appender.DRFA.MaxFileSize=10MBnnn1/' log4j.properties


            With respect to the duplicating of lines. You're not going to have this appender defined more than 1 time:



            appender.DRFA.DatePattern


            Therefore we can look more explicitly for this occurrence, rather than the DatePattern line that you're looking for.



            Example run



            Here's a sample file that has just this line in it:



            $ cat log4j.properties
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            Run the sed command above against this file:



            $ cat log4j.properties
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd





            share|improve this answer



















            • 1




              @yael - I updated the answer so that it includes them now.
              – slm♦
              7 hours ago













            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            If I understand your question correctly you merely want to replace this line:



            log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
            -or-
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            With these lines:



            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
            -or-
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            This GNU sed command can do this:



            $ sed -i 's/(.*appender.DRFA.DatePattern=.*)/log4j.appender.DRFA=org.apache.log4j.RollingFileAppendernlog4j.appender.DRFA.MaxBackupIndex=100nlog4j.appender.DRFA.MaxFileSize=10MBnnn1/' log4j.properties


            With respect to the duplicating of lines. You're not going to have this appender defined more than 1 time:



            appender.DRFA.DatePattern


            Therefore we can look more explicitly for this occurrence, rather than the DatePattern line that you're looking for.



            Example run



            Here's a sample file that has just this line in it:



            $ cat log4j.properties
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            Run the sed command above against this file:



            $ cat log4j.properties
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd





            share|improve this answer















            If I understand your question correctly you merely want to replace this line:



            log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
            -or-
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            With these lines:



            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
            -or-
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            This GNU sed command can do this:



            $ sed -i 's/(.*appender.DRFA.DatePattern=.*)/log4j.appender.DRFA=org.apache.log4j.RollingFileAppendernlog4j.appender.DRFA.MaxBackupIndex=100nlog4j.appender.DRFA.MaxFileSize=10MBnnn1/' log4j.properties


            With respect to the duplicating of lines. You're not going to have this appender defined more than 1 time:



            appender.DRFA.DatePattern


            Therefore we can look more explicitly for this occurrence, rather than the DatePattern line that you're looking for.



            Example run



            Here's a sample file that has just this line in it:



            $ cat log4j.properties
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd


            Run the sed command above against this file:



            $ cat log4j.properties
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            #log4j.appender.DRFA.DatePattern=.yyyy-MM-dd






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited 6 hours ago









            Kusalananda

            100k13199310




            100k13199310











            answered 7 hours ago









            slm♦

            232k65479648




            232k65479648







            • 1




              @yael - I updated the answer so that it includes them now.
              – slm♦
              7 hours ago













            • 1




              @yael - I updated the answer so that it includes them now.
              – slm♦
              7 hours ago








            1




            1




            @yael - I updated the answer so that it includes them now.
            – slm♦
            7 hours ago





            @yael - I updated the answer so that it includes them now.
            – slm♦
            7 hours ago













            up vote
            5
            down vote













            Using awk:



            awk 'done != 1 && /DatePattern/ 
            print "log4j.appender.DRFA=org.apache.log4j.RollingFileAppender"
            print "log4j.appender.DRFA.MaxBackupIndex=100"
            print "log4j.appender.DRFA.MaxFileSize=10MB"
            done = 1
            1' file >newfile && mv newfile file


            This would print the three lines when the first match of DatePattern occurs in the file. The flag done is then set to 1 which stops the lines from being printed again. The trailing 1 at the very end causes every line in the in-data to be printed.



            If you want empty lines in the output after the three lines, add nn to the end of the last string.



            The output is written to newfile and if awk did not encounter any strange errors, the original is then replaced by this once the awk process terminates.






            share|improve this answer























            • good solution , but I prefer to update the file itself like "sed -i" and not copy the file to the source file for update , is it possible with awk ?
              – yael
              8 hours ago










            • @yael A standard redirection and mv. See updated answer. Not even GNU awk has an in-place editing option. It's not something that awk program usually do.
              – Kusalananda
              8 hours ago







            • 1




              GNU awk has it...
              – don_crissti
              7 hours ago










            • @don_crissti :-) That's an extension, even to GNU awk.
              – Kusalananda
              7 hours ago






            • 1




              Sure but that doesn't change the fact that it exists... Anyway, +1 for answering the actual question here...
              – don_crissti
              7 hours ago














            up vote
            5
            down vote













            Using awk:



            awk 'done != 1 && /DatePattern/ 
            print "log4j.appender.DRFA=org.apache.log4j.RollingFileAppender"
            print "log4j.appender.DRFA.MaxBackupIndex=100"
            print "log4j.appender.DRFA.MaxFileSize=10MB"
            done = 1
            1' file >newfile && mv newfile file


            This would print the three lines when the first match of DatePattern occurs in the file. The flag done is then set to 1 which stops the lines from being printed again. The trailing 1 at the very end causes every line in the in-data to be printed.



            If you want empty lines in the output after the three lines, add nn to the end of the last string.



            The output is written to newfile and if awk did not encounter any strange errors, the original is then replaced by this once the awk process terminates.






            share|improve this answer























            • good solution , but I prefer to update the file itself like "sed -i" and not copy the file to the source file for update , is it possible with awk ?
              – yael
              8 hours ago










            • @yael A standard redirection and mv. See updated answer. Not even GNU awk has an in-place editing option. It's not something that awk program usually do.
              – Kusalananda
              8 hours ago







            • 1




              GNU awk has it...
              – don_crissti
              7 hours ago










            • @don_crissti :-) That's an extension, even to GNU awk.
              – Kusalananda
              7 hours ago






            • 1




              Sure but that doesn't change the fact that it exists... Anyway, +1 for answering the actual question here...
              – don_crissti
              7 hours ago












            up vote
            5
            down vote










            up vote
            5
            down vote









            Using awk:



            awk 'done != 1 && /DatePattern/ 
            print "log4j.appender.DRFA=org.apache.log4j.RollingFileAppender"
            print "log4j.appender.DRFA.MaxBackupIndex=100"
            print "log4j.appender.DRFA.MaxFileSize=10MB"
            done = 1
            1' file >newfile && mv newfile file


            This would print the three lines when the first match of DatePattern occurs in the file. The flag done is then set to 1 which stops the lines from being printed again. The trailing 1 at the very end causes every line in the in-data to be printed.



            If you want empty lines in the output after the three lines, add nn to the end of the last string.



            The output is written to newfile and if awk did not encounter any strange errors, the original is then replaced by this once the awk process terminates.






            share|improve this answer















            Using awk:



            awk 'done != 1 && /DatePattern/ 
            print "log4j.appender.DRFA=org.apache.log4j.RollingFileAppender"
            print "log4j.appender.DRFA.MaxBackupIndex=100"
            print "log4j.appender.DRFA.MaxFileSize=10MB"
            done = 1
            1' file >newfile && mv newfile file


            This would print the three lines when the first match of DatePattern occurs in the file. The flag done is then set to 1 which stops the lines from being printed again. The trailing 1 at the very end causes every line in the in-data to be printed.



            If you want empty lines in the output after the three lines, add nn to the end of the last string.



            The output is written to newfile and if awk did not encounter any strange errors, the original is then replaced by this once the awk process terminates.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited 8 hours ago


























            answered 8 hours ago









            Kusalananda

            100k13199310




            100k13199310











            • good solution , but I prefer to update the file itself like "sed -i" and not copy the file to the source file for update , is it possible with awk ?
              – yael
              8 hours ago










            • @yael A standard redirection and mv. See updated answer. Not even GNU awk has an in-place editing option. It's not something that awk program usually do.
              – Kusalananda
              8 hours ago







            • 1




              GNU awk has it...
              – don_crissti
              7 hours ago










            • @don_crissti :-) That's an extension, even to GNU awk.
              – Kusalananda
              7 hours ago






            • 1




              Sure but that doesn't change the fact that it exists... Anyway, +1 for answering the actual question here...
              – don_crissti
              7 hours ago
















            • good solution , but I prefer to update the file itself like "sed -i" and not copy the file to the source file for update , is it possible with awk ?
              – yael
              8 hours ago










            • @yael A standard redirection and mv. See updated answer. Not even GNU awk has an in-place editing option. It's not something that awk program usually do.
              – Kusalananda
              8 hours ago







            • 1




              GNU awk has it...
              – don_crissti
              7 hours ago










            • @don_crissti :-) That's an extension, even to GNU awk.
              – Kusalananda
              7 hours ago






            • 1




              Sure but that doesn't change the fact that it exists... Anyway, +1 for answering the actual question here...
              – don_crissti
              7 hours ago















            good solution , but I prefer to update the file itself like "sed -i" and not copy the file to the source file for update , is it possible with awk ?
            – yael
            8 hours ago




            good solution , but I prefer to update the file itself like "sed -i" and not copy the file to the source file for update , is it possible with awk ?
            – yael
            8 hours ago












            @yael A standard redirection and mv. See updated answer. Not even GNU awk has an in-place editing option. It's not something that awk program usually do.
            – Kusalananda
            8 hours ago





            @yael A standard redirection and mv. See updated answer. Not even GNU awk has an in-place editing option. It's not something that awk program usually do.
            – Kusalananda
            8 hours ago





            1




            1




            GNU awk has it...
            – don_crissti
            7 hours ago




            GNU awk has it...
            – don_crissti
            7 hours ago












            @don_crissti :-) That's an extension, even to GNU awk.
            – Kusalananda
            7 hours ago




            @don_crissti :-) That's an extension, even to GNU awk.
            – Kusalananda
            7 hours ago




            1




            1




            Sure but that doesn't change the fact that it exists... Anyway, +1 for answering the actual question here...
            – don_crissti
            7 hours ago




            Sure but that doesn't change the fact that it exists... Anyway, +1 for answering the actual question here...
            – don_crissti
            7 hours ago










            up vote
            3
            down vote













            Using GNU sed you may approach it this way also:



            sed -i -e '
            /DatePattern/!b

            i
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB

            :a;n;$!ba
            ' input.file


            And if you are sure that the DatePattern cannot be on the last line of the file, then you could do this also:



            sed -i -e '
            /DatePattern/!b
            r file2add.txt
            N;:a;n;$!ba
            ' input.file


            where you put all the lines to be added into a file, say file2add.txt. Note: with this method, you don't put trailing backslashes in the file file2add.txt.






            share|improve this answer





















            • Yes, those are both proper ways to do it with sed. Future visitors should keep in mind that i requires any embedded backslashes to be escaped.
              – don_crissti
              7 hours ago














            up vote
            3
            down vote













            Using GNU sed you may approach it this way also:



            sed -i -e '
            /DatePattern/!b

            i
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB

            :a;n;$!ba
            ' input.file


            And if you are sure that the DatePattern cannot be on the last line of the file, then you could do this also:



            sed -i -e '
            /DatePattern/!b
            r file2add.txt
            N;:a;n;$!ba
            ' input.file


            where you put all the lines to be added into a file, say file2add.txt. Note: with this method, you don't put trailing backslashes in the file file2add.txt.






            share|improve this answer





















            • Yes, those are both proper ways to do it with sed. Future visitors should keep in mind that i requires any embedded backslashes to be escaped.
              – don_crissti
              7 hours ago












            up vote
            3
            down vote










            up vote
            3
            down vote









            Using GNU sed you may approach it this way also:



            sed -i -e '
            /DatePattern/!b

            i
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB

            :a;n;$!ba
            ' input.file


            And if you are sure that the DatePattern cannot be on the last line of the file, then you could do this also:



            sed -i -e '
            /DatePattern/!b
            r file2add.txt
            N;:a;n;$!ba
            ' input.file


            where you put all the lines to be added into a file, say file2add.txt. Note: with this method, you don't put trailing backslashes in the file file2add.txt.






            share|improve this answer













            Using GNU sed you may approach it this way also:



            sed -i -e '
            /DatePattern/!b

            i
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB

            :a;n;$!ba
            ' input.file


            And if you are sure that the DatePattern cannot be on the last line of the file, then you could do this also:



            sed -i -e '
            /DatePattern/!b
            r file2add.txt
            N;:a;n;$!ba
            ' input.file


            where you put all the lines to be added into a file, say file2add.txt. Note: with this method, you don't put trailing backslashes in the file file2add.txt.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered 7 hours ago









            Rakesh Sharma

            2933




            2933











            • Yes, those are both proper ways to do it with sed. Future visitors should keep in mind that i requires any embedded backslashes to be escaped.
              – don_crissti
              7 hours ago
















            • Yes, those are both proper ways to do it with sed. Future visitors should keep in mind that i requires any embedded backslashes to be escaped.
              – don_crissti
              7 hours ago















            Yes, those are both proper ways to do it with sed. Future visitors should keep in mind that i requires any embedded backslashes to be escaped.
            – don_crissti
            7 hours ago




            Yes, those are both proper ways to do it with sed. Future visitors should keep in mind that i requires any embedded backslashes to be escaped.
            – don_crissti
            7 hours ago










            up vote
            2
            down vote













            Since you tagged this query with perl also, so here goes one obvious way:



            perl -pi -e '
            print <<EOF if /DatePattern/ && !$seen++;
            log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
            log4j.appender.DRFA.MaxBackupIndex=100
            log4j.appender.DRFA.MaxFileSize=10MB
            EOF
            ' input.file


            Note: The added text mustn't contain the standalone string EOF on a line by itself. The backslashing of the heredoc is necessary , lest any variables get expanded .



            If you cannot be sure of the above constraints in the text to add, then do this:



            perl -pi -e '
            next if !/DatePattern/ || $seen++;
            open my $fh, "<", "file2add.txt" or die "Could not open file for reading:$!n";
            $_ = join "", <$fh>, $_;
            ' input.file





            share|improve this answer

























              up vote
              2
              down vote













              Since you tagged this query with perl also, so here goes one obvious way:



              perl -pi -e '
              print <<EOF if /DatePattern/ && !$seen++;
              log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
              log4j.appender.DRFA.MaxBackupIndex=100
              log4j.appender.DRFA.MaxFileSize=10MB
              EOF
              ' input.file


              Note: The added text mustn't contain the standalone string EOF on a line by itself. The backslashing of the heredoc is necessary , lest any variables get expanded .



              If you cannot be sure of the above constraints in the text to add, then do this:



              perl -pi -e '
              next if !/DatePattern/ || $seen++;
              open my $fh, "<", "file2add.txt" or die "Could not open file for reading:$!n";
              $_ = join "", <$fh>, $_;
              ' input.file





              share|improve this answer























                up vote
                2
                down vote










                up vote
                2
                down vote









                Since you tagged this query with perl also, so here goes one obvious way:



                perl -pi -e '
                print <<EOF if /DatePattern/ && !$seen++;
                log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
                log4j.appender.DRFA.MaxBackupIndex=100
                log4j.appender.DRFA.MaxFileSize=10MB
                EOF
                ' input.file


                Note: The added text mustn't contain the standalone string EOF on a line by itself. The backslashing of the heredoc is necessary , lest any variables get expanded .



                If you cannot be sure of the above constraints in the text to add, then do this:



                perl -pi -e '
                next if !/DatePattern/ || $seen++;
                open my $fh, "<", "file2add.txt" or die "Could not open file for reading:$!n";
                $_ = join "", <$fh>, $_;
                ' input.file





                share|improve this answer













                Since you tagged this query with perl also, so here goes one obvious way:



                perl -pi -e '
                print <<EOF if /DatePattern/ && !$seen++;
                log4j.appender.DRFA=org.apache.log4j.RollingFileAppender
                log4j.appender.DRFA.MaxBackupIndex=100
                log4j.appender.DRFA.MaxFileSize=10MB
                EOF
                ' input.file


                Note: The added text mustn't contain the standalone string EOF on a line by itself. The backslashing of the heredoc is necessary , lest any variables get expanded .



                If you cannot be sure of the above constraints in the text to add, then do this:



                perl -pi -e '
                next if !/DatePattern/ || $seen++;
                open my $fh, "<", "file2add.txt" or die "Could not open file for reading:$!n";
                $_ = join "", <$fh>, $_;
                ' input.file






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered 6 hours ago









                Rakesh Sharma

                2933




                2933






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460684%2finsert-multiline-text-before-the-first-matching-line-in-a-file%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Color the edges and diagonals of a regular polygon

                    Relationship between determinant of matrix and determinant of adjoint?

                    What is the equation of a 3D cone with generalised tilt?