我尝试使用这个命令,但它不为我工作。如果我执行下面的命令,
#!/bin/bash
# set the path to your file
file_path="/etc/nginx/nginx.conf"
# set the word you want to search for
search_word="Settings for a TLS enabled server"
# get the line number where the word is found
line_number=$(grep -n "$search_word" $file_path | cut -d: -f1)
# calculate the line number where you want to insert the new line
insert_line=$((line_number-10))
USAGE=$(cat <<-END
location /nginx-status {
stub_status on;
allow all;
}
END
)
# insert the new line using sed
sed -i "${insert_line}i $USAGE" $file_path
我需要在Nginx.conf文件中的“启用TLS的服务器的设置”中添加以下行,10行之前。
location /nginx-status {
stub_status on;
allow all;
}
展望未来,如下图所示。Expecting_result
2条答案
按热度按时间xkrw2x1b1#
试试这个:
sed -i '10ilocation /nginx-status {\nstub_status on; \nallow all; \n}' $path/nginx.conf
编辑:这很有效:
sed -i "$(echo $insert_line)i $(echo $USAGE)" $file_path
np8igboo2#
最简单的方法是将简单的awk与
tac
结合使用