我刚开始使用 Linux,我需要注释/取消注释 .yml 文件的某些行。我已经搜索了文档,并在这里通过不同的帖子搜索了这个,例如:comment a line with sed
我需要从这里注释第 1 行和第 3 行:
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["localhost:9200"]
并从此处取消注释第 1 行和第 3 行:
#output.logstash:
# The Logstash hosts
#hosts: ["localhost:5044"]
我使用了这些命令:
sudo sed -i '/output.elasticsearch/s/^/#/g' /etc/filebeat/filebeat.yml
sudo sed -i '/["localhost:9200"]/s/^/#/g' /etc/filebeat/filebeat.yml
sudo sed -i '/output.logstash/s/^#//g' /etc/filebeat/filebeat.yml
sudo sed -i '/["localhost:5044"]/s/^ #//g' /etc/filebeat/filebeat.yml
但是,虽然这适用于每个块的第 1 行,但它不适用于第 3 行,其中 # 符号前有空格,我不知道如何更改