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_PATH
hasta 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_PATH
no 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_PATH
línea donde terminaría el script. La línea con la cadena no se imprimió debido a que la -n
opción deshabilita la salida predeterminada normal. La ventaja de esto es que sed
no 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 awk
leería
awk '/EXPORTER_JAR_PATH/ { exit } { print }' file
o, más corto,
awk '/EXPORTER_JAR_PATH/ { exit }; 1' file