Insert multiline text before the first matching line in a file
Clash 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
text-processing awk sed logs perl
add a comment |Â
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
text-processing awk sed logs perl
add a comment |Â
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
text-processing awk sed logs perl
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
text-processing awk sed logs perl
edited 7 hours ago
don_crissti
46k15119151
46k15119151
asked 8 hours ago
yael
1,787940
1,787940
add a comment |Â
add a comment |Â
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
1
@yael - I updated the answer so that it includes them now.
â slmâ¦
7 hours ago
add a comment |Â
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.
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 andmv
. See updated answer. Not even GNUawk
has an in-place editing option. It's not something thatawk
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 GNUawk
.
â 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
add a comment |Â
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
.
Yes, those are both proper ways to do it withsed
. Future visitors should keep in mind thati
requires any embedded backslashes to be escaped.
â don_crissti
7 hours ago
add a comment |Â
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
add a comment |Â
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
1
@yael - I updated the answer so that it includes them now.
â slmâ¦
7 hours ago
add a comment |Â
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
1
@yael - I updated the answer so that it includes them now.
â slmâ¦
7 hours ago
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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.
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 andmv
. See updated answer. Not even GNUawk
has an in-place editing option. It's not something thatawk
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 GNUawk
.
â 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
add a comment |Â
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.
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 andmv
. See updated answer. Not even GNUawk
has an in-place editing option. It's not something thatawk
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 GNUawk
.
â 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
add a comment |Â
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.
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.
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 andmv
. See updated answer. Not even GNUawk
has an in-place editing option. It's not something thatawk
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 GNUawk
.
â 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
add a comment |Â
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 andmv
. See updated answer. Not even GNUawk
has an in-place editing option. It's not something thatawk
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 GNUawk
.
â 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
add a comment |Â
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
.
Yes, those are both proper ways to do it withsed
. Future visitors should keep in mind thati
requires any embedded backslashes to be escaped.
â don_crissti
7 hours ago
add a comment |Â
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
.
Yes, those are both proper ways to do it withsed
. Future visitors should keep in mind thati
requires any embedded backslashes to be escaped.
â don_crissti
7 hours ago
add a comment |Â
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
.
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
.
answered 7 hours ago
Rakesh Sharma
2933
2933
Yes, those are both proper ways to do it withsed
. Future visitors should keep in mind thati
requires any embedded backslashes to be escaped.
â don_crissti
7 hours ago
add a comment |Â
Yes, those are both proper ways to do it withsed
. Future visitors should keep in mind thati
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered 6 hours ago
Rakesh Sharma
2933
2933
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password