alle Zeilen von der Übereinstimmungszeile bis zum Ende löschen, einschließlich der Übereinstimmungszeile

alle Zeilen von der Übereinstimmungszeile bis zum Ende löschen, einschließlich der Übereinstimmungszeile

EXPORTER_JAR_PATHDies ist die Datei, in der wir alle Zeilen ab der Zeile, die „ “ enthält, bis zum Dateiende löschen möchten

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

dies ist meine Lösung zum Löschen der Zeilen aus der Zeile, die - EXPORTER_JAR_PATH bis zum Ende enthalten.

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

Wie wir sehen können, ist die Linie -Export EXPORTER_JAR_PATH=/tmp/hgt.yml

Existiert es noch, wo liegen wir falsch?

Erwartete Ausgabe

# 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

Antwort1

sed '/EXPORTER_JAR_PATH/,$d' file

Dadurch werden alle Zeilen ab der ersten Zeile, die die Teilzeichenfolge enthält, EXPORTER_JAR_PATHbis einschließlich dem Ende der Datei ( $adressiert das Ende der Datei) gelöscht.

Ihr Befehl löscht alle Zeilen, die nicht im Bereich zwischen Zeile 1 und der Zeile liegen, die diese Zeichenfolge enthält. Dies bedeutet, dass die Zeile, die die Teilzeichenfolge enthält, EXPORTER_JAR_PATHnicht gelöscht wird.

Alternativ, wie in den Kommentaren unten von Paul_Pendant und mosvy angemerkt,

sed -n '/EXPORTER_JAR_PATH/q;p' file

wodurch explizit mit pjede Zeile bis zur Zeile ausgegeben wird, EXPORTER_JAR_PATHin der das Skript beendet wird. Die Zeile mit der Zeichenfolge wird nicht ausgegeben, da die -nOption die normale Standardausgabe deaktiviert. Der Vorteil davon ist, dass sednicht die gesamte Datei gelesen werden muss (in diesem speziellen Fall würde es jedoch keinen drastischen Unterschied machen, da die Datei so kurz ist).

Das Äquivalent zu awkwürde lauten

awk '/EXPORTER_JAR_PATH/ { exit } { print }' file

oder, kürzer,

awk '/EXPORTER_JAR_PATH/ { exit }; 1' file

verwandte Informationen