The following awk
syntax will add the 3 lines in the file before the line with the word - 'DatePattern':
$ 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
the problem is that awk
does not care if the lines already exist, so what needs to be added to the awk
, in order to insert the lines only if lines are not already present?
Other example
In this case we want to add before the line with word 'HOTEL' the names 'trump', 'bush', & 'putin', but only in the case where the names do not exists:
$ awk 'done != 1 && /HOTEL/ {
print "trump"
print "bush"
print "putin"
done = 1
} 1' file >newfile && mv newfile file