BASH Adding a server name to Apache.conf via AWK

Hi guys, I'm trying to automate the build and deployment, and realized that I can’t install correctly ServerNamein

# Global configuration
#

for some stupid reason awk doesn't interpret mine. \nIf I delete \n#, it works fine, but I would like it to be under a global comment and not in the middle, what am I doing wrong here?

Hoping to see

# Global configuration
#

ServerName localhost

Here is the expression

awk '/# Global configuration\n#/ { print; print "\nServerName localhost"; next }1' apache2.conf

Maybe it's best to use SED?

+4
source share
2 answers

awk - to handle strings. You can match a pattern contained in only one line.

What you are trying to do can be done with:

awk '/# Global configuration/{c=1;print;next} c&&/^#/ {print; print "\nServerName localhost"; c=0; next} 1' file

# Global configuration
#

ServerName localhost
+2

:

awk 'p&&!NF{print "ServerName localhost";p=0}/# Global configuration/{p=1}1' apache2.conf
+3

Source: https://habr.com/ru/post/1535079/


All Articles