这是我们要删除包含“ EXPORTER_JAR_PATH
”的行直到文件末尾的所有行的文件
more ambari-agent.ini
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific
export EXPORTER_JAR_PATH=/tmp/hgt.yml
[server]
hostname=master.sys65.com
url_port=8440
secured_url_port=8441
connect_retry_delay=10
max_reconnect_retry_delay=30
EXPORTER_JAR_PATH
这是我的解决方案,从包含 -直到结束的行中删除行
sed '1,/EXPORTER_JAR_PATH/!d' ambari-agent.ini
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific
export EXPORTER_JAR_PATH=/tmp/hgt.yml
正如我们所看到的那行——Export
EXPORTER_JAR_PATH=/tmp/hgt.yml
依然存在,我们哪里错了?
预期产出
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific
答案1
sed '/EXPORTER_JAR_PATH/,$d' file
这将删除从包含子字符串的第一行EXPORTER_JAR_PATH
到文件末尾($
地址文件末尾)的所有行。
命令的作用是删除不在第 1 行和包含该字符串的行(包括第 1 行)之间范围内的所有行。这意味着包含子字符串的行EXPORTER_JAR_PATH
不会被删除。
或者,正如 Paul_Pendant 和 mosvy 在下面的评论中指出的那样,
sed -n '/EXPORTER_JAR_PATH/q;p' file
它将显式地p
打印出每一行,直到到达EXPORTER_JAR_PATH
脚本将终止的行。由于该-n
选项禁用普通默认输出,因此不会打印包含字符串的行。这样做的好处是sed
不必读取整个文件(但是,在这种特定情况下,由于文件太短,因此不会产生很大的差异)。
awk
与等效的内容是:
awk '/EXPORTER_JAR_PATH/ { exit } { print }' file
或者,更短,
awk '/EXPORTER_JAR_PATH/ { exit }; 1' file