eliminar todas las líneas desde la línea coincidente hasta el final incluir la línea coincidente

eliminar todas las líneas desde la línea coincidente hasta el final incluir la línea coincidente

este es el archivo que queremos eliminar todas las líneas de la línea que incluyen " EXPORTER_JAR_PATH" hasta el final del archivo

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

esta es mi solución para eliminar las líneas de la línea que incluyen - EXPORTER_JAR_PATH hasta el final

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

Como podemos ver la línea -Export EXPORTER_JAR_PATH=/tmp/hgt.yml

Todavía existe, ¿en qué nos equivocamos?

Rendimiento esperado

# 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

Respuesta1

sed '/EXPORTER_JAR_PATH/,$d' file

Esto eliminaría todas las líneas desde la primera línea que contiene la subcadena EXPORTER_JAR_PATHhasta el final del archivo ( $dirige el final del archivo), inclusive.

Lo que hace tu comando es eliminar todas las líneas que no se encuentran dentro del rango entre la línea 1 y la línea que contiene esa cadena, inclusive. Esto significa que la línea que contiene la subcadena EXPORTER_JAR_PATHno se eliminará.

Alternativamente, como se señala en los comentarios a continuación tanto de Paul_Pendant como de Mosvy,

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

que explícitamente, con p, imprimiría cada línea hasta llegar a la EXPORTER_JAR_PATHlínea donde terminaría el script. La línea con la cadena no se imprimió debido a que la -nopción deshabilita la salida predeterminada normal. La ventaja de esto es que sedno tendría que leer todo el archivo (sin embargo, no haría una diferencia drástica en este caso específico ya que el archivo es muy corto).

Lo equivalente a awkleería

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

o, más corto,

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

información relacionada