I have a big apache configuration file and in each of the virtualhost sections, I want to add its own log entry. I am wondering if I can do it with a script.
My current configuration file is something like this;
ServerName abc.com
some information.
…
……
and I want to have it something like;
ServerName abc.com
CustomLog "/usr/local/logs/abc.com.log"
some information.
…
……
Is it possible by some sort of script? I have lots and lots of such virtualhost entries, so manually updating is impossible.. Any ideas?
awkcan be much simpler to use.NRis a built-in variable that tracks line numbers.-vand avariable nameto pass values dynamically instead of hard-coding it in the script. eg.awk -v line="$var" 'NR==line{print "my log"}1' INPUT_FILE. In this case,lineis an awk variable and$varcan be your bash variable defined outside ofawk'sscope.Test:
In the comments I saw your question of adding your log after 3 lines starting from the matched pattern (ServerName, in this example). For that you can try something like this –
awk '/ServerName/{a=NR;print;next} NR==(a+3){print$0;print "my log";next}1' file