I have a file like this
<VirtualHost my.shop:80>
ServerAdmin root@localhost
DocumentRoot /tpm
ServerAlias *.*.*
Alias /box /home/shop/box
SetEnv BOX_ROOT /home/shop/pen/www
</VirtualHost>
<VirtualHost my.shop:80>
ServerAdmin root@localhost
DocumentRoot /tpm
ServerAlias *.*.*
Alias /pen /home/shop/pen
Alias /book /home/shop/book
SetEnv PEN_ROOT /home/shop/pen/www
SetEnv BOOK_ROOT /home/shop/book/www
</VirtualHost>
<VirtualHost my.shop:80>
ServerAdmin root@localhost
DocumentRoot /tpm
ServerAlias *.*.*
Alias /water /home/shop/water
Alias /computer /home/shop/computer
SetEnv WATER_ROOT /home/shop/water/www
SetEnv COMPUTER_ROOT /home/shop/cpmputer/www
</VirtualHost>
<VirtualHost my.shop:80>
ServerAdmin root@localhost
DocumentRoot /tpm
ServerAlias *.*.*
Alias /pen /home/shop/pen
Alias /book /home/shop/book
SetEnv PEN_ROOT /home/shop/pen/www
SetEnv BOOK_ROOT /home/shop/book/www
</VirtualHost>
I need to add several lines knowing
- it need to match a
VirtualHost
containing the string pen
- if there is a match, add below line in its
VirtualHost
and insert after the line VirtualHost my.shop:80
- there might be one to many matches in one file
Here is the example of lines to insert
this
is
show
test
The expected result should be
<VirtualHost my.shop:80>
ServerAdmin root@localhost
DocumentRoot /tpm
ServerAlias *.*.*
Alias /box /home/shop/box
SetEnv BOX_ROOT /home/shop/pen/www
</VirtualHost>
<VirtualHost my.shop:80>
this
is
show
test
ServerAdmin root@localhost
DocumentRoot /tpm
ServerAlias *.*.*
Alias /pen /home/shop/pen
Alias /book /home/shop/book
SetEnv PEN_ROOT /home/shop/pen/www
SetEnv BOOK_ROOT /home/shop/book/www
</VirtualHost>
<VirtualHost my.shop:80>
ServerAdmin root@localhost
DocumentRoot /tpm
ServerAlias *.*.*
Alias /water /home/shop/water
Alias /computer /home/shop/computer
SetEnv WATER_ROOT /home/shop/water/www
SetEnv COMPUTER_ROOT /home/shop/cpmputer/www
</VirtualHost>
<VirtualHost my.shop:80>
this
is
show
test
ServerAdmin root@localhost
DocumentRoot /tpm
ServerAlias *.*.*
Alias /pen /home/shop/pen
Alias /book /home/shop/book
SetEnv PEN_ROOT /home/shop/pen/www
SetEnv BOOK_ROOT /home/shop/book/www
</VirtualHost>
I don't know how I can use lineinfile
when there are matches for pen
and insertafter
in specific.
question from:
https://stackoverflow.com/questions/66060187/lineinfile-match-string-and-insertafter-specific-place 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…