找到一个模式并在 shell 脚本中替换它的值

找到一个模式并在 shell 脚本中替换它的值

我一直在尝试使用 shell 脚本对我的配置文件进行以下更改。这是我的以下弹性搜索配置文件。

需要注释掉下面的行并替换其值。

#network.host: 192.168.0.1

并在网络部分添加这些行

transport.host: localhost
transport.tcp.port: 9300

我怎样才能实现这个目标?

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#

答案1

你可以用sed它来做。

第一部分非常简单,找到一个带注释的network.host并将其替换为具有不同值的未注释的网络主机可以通过以下方式完成:

sed -i -e 's/^#network\.host: .*/network.host: 1.2.3.4/' "${ES_HOME}/config/elasticsearch.yml"

-i选项会进行就地修改,因此它将替换您当前的elasticsearch.yml文件。 (您可以保存它的备份,例如elasticsearch.yml.bak使用-i.bak替代方法。)

的参数-e是 sed 脚本,在本例中是带有搜索/替换命令的正则表达式。它匹配注释行,#network.host以包含 IP 的未注释行开头并将其替换。

如果您想从环境变量中获取 IP 或主机名,可以通过将“...”字符串分成两部分并在其中插入外部变量来实现:

sed -i -e 's/^#network\.host: .*/network.host: '"${ip_address}"'/' "${ES_HOME}/config/elasticsearch.yml"

但请注意,这可能很脆弱...如果 的内容${ip_address}包含一个/字符,这将破坏 sed 命令...

对于第二部分,插入 Transport.host 行,您可以使用 sed 的i\命令在您匹配的行之前插入一行。例如,您可以匹配网络部分中的最后一个注释(“...查阅网络模块文档”)并插入其中。当您插入多行时,您需要启动一个新块,{以便可以运行多个命令。

这应该可以做到(请注意,这是一个跨越多行的命令!):

sed -i -e '
/consult the network module documentation/{
i\
# Set custom transport settings:
i\
#
i\
transport.host: localhost
i\
transport.tcp.port: 9300
i\
#
}' "${ES_HOME}/config/elasticsearch.yml"

现在我们可以将它们放在一起,并且还添加一个检查以跳过插入(如果之前已经完成)。我们可以通过查找插入的注释(“设置自定义传输设置”)并使用命令b跳到脚本末尾,在这种情况下跳过以下编辑来做到这一点。

最终的脚本是:

# Set your own IP into ${ip_address} however you have to.
ip_address=1.2.3.4
sed -i -e '
s/^#network\.host: .*/network.host: '"${ip_address}"'/
/^# Set custom transport settings/,$b
/consult the network module documentation/{
i\
# Set custom transport settings:
i\
#
i\
transport.host: localhost
i\
transport.tcp.port: 9300
i\
#
}' "${ES_HOME}/config/elasticsearch.yml"

相关内容