linux - search and add a pattern in a big file -
linux - search and add a pattern in a big file -
i have big apache configuration file , in each of virtualhost sections, want add together own log entry. wondering if can script.
my current configuration file this;
servername abc.com information. … …… and want have like;
servername abc.com customlog "/usr/local/logs/abc.com.log" information. … …… is possible sort of script? have lots , lots of such virtualhost entries, manually updating impossible.. ideas?
awk can much simpler use.
awk 'nr==3{print "my log"}1' input_file nr built-in variable tracks line numbers. you can utilize -v , variable name pass values dynamically instead of hard-coding in script. eg. awk -v line="$var" 'nr==line{print "my log"}1' input_file. in case, line awk variable , $var can bash variable defined outside of awk's scope. test: [jaypal:~/temp] cat file servername abc.com information. … …… [jaypal:~/temp] awk 'nr==3{print "my log"}1' file # add together log after 2 lines servername abc.com information. log … …… [jaypal:~/temp] awk 'nr==4{print "my log"}1' file # add together log after 3 lines servername abc.com information. … log …… [jaypal:~/temp] var=2 # define variable holds line number want print on [jaypal:~/temp] awk -v line="$var" 'nr==line{print "my log"}1' file servername abc.com log information. … …… in comments saw question of adding log after 3 lines starting matched pattern (servername, in example). can seek -
awk '/servername/{a=nr;print;next} nr==(a+3){print$0;print "my log";next}1' file
[jaypal:~/temp] awk '/servername/{a=nr;print;next} nr==(a+3){print$0;print "my log";next}1' file servername abc.com information. … …… log linux perl shell unix awk
Comments
Post a Comment