這是我們要刪除包含“ 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