Skip to main content
added 506 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

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 \n\n 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.


Requested in comments: Adding the lines after the matched line,

awk '1; 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
    }' file

This moves the 1 (which does the printing of each input line and could be replaced by { print }) to before the code that is triggered when the pattern matches.

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 \n\n 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.

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 \n\n 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.


Requested in comments: Adding the lines after the matched line,

awk '1; 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
    }' file

This moves the 1 (which does the printing of each input line and could be replaced by { print }) to before the code that is triggered when the pattern matches.

added 188 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

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 \n\n 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.

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

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 \n\n to the end of the last string.

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 \n\n 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.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

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

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 \n\n to the end of the last string.