在nginx.conf文件中的单词“server {”后面添加以下行。
location /nginx-status {
stub_status on;
allow all;
}
使用下面的脚本,在单词“server {”出现的任何位置,在该单词旁边添加一个新行。
$Nginx_home = "I:\Anand\nginx-1.22.1"
$filePath = "$Nginx_home\conf\nginx.conf"
$textToAdd1 = {
location /nginx-status {
stub_status on;
allow all;
}
}
$content = Get-Content $filePath
# Replace the specific word with the word followed by a new line character
$content = $content -replace "server {", "server {`n$textToAdd1"
# Write the updated contents back to the text file
Set-Content $filePath $content
nginx.conf文件包含多个单词“server {"。但是我需要先找到这个单词,然后将下面的行添加到下一行。
location /nginx-status {
stub_status on;
allow all;
}
2条答案
按热度按时间qq24tv8q1#
使用
ForEach-Object
检查通过管道的每一行,在您要查找的行后面添加额外的内容:u4vypkhs2#
我将使用
switch
来完成此操作,并跟踪您是否已经插入了新文本,以便只插入一次: