excluir todas as linhas da linha correspondente até o final incluir a linha correspondente

excluir todas as linhas da linha correspondente até o final incluir a linha correspondente

este é o arquivo que queremos deletar todas as linhas da linha que inclui “ EXPORTER_JAR_PATH” até o final do arquivo

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 é a minha solução para excluir as linhas da linha que incluem - EXPORTER_JAR_PATH até o 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 a linha -Export EXPORTER_JAR_PATH=/tmp/hgt.yml

Ainda existe, onde estamos errados?

Resultado 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

Responder1

sed '/EXPORTER_JAR_PATH/,$d' file

Isso excluiria todas as linhas da primeira linha que contém a substring EXPORTER_JAR_PATHaté o final do arquivo ( $endereça o final do arquivo), inclusive.

O que o seu comando faz é excluir todas as linhas que não estão no intervalo entre a linha 1 e a linha que contém essa string, inclusive. Isso significa que a linha que contém a substring EXPORTER_JAR_PATHnão seria excluída.

Alternativamente, conforme apontado nos comentários abaixo por Paul_Pendant e mosvy,

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

que imprimiria explicitamente, com p, cada linha até chegar à EXPORTER_JAR_PATHlinha onde o script terminaria. A linha com a string não seria impressa devido à -nopção de desabilitar a saída padrão comum. A vantagem disso é que sednão seria necessário ler o arquivo inteiro (no entanto, não faria uma diferença drástica neste caso específico, pois o arquivo é muito curto).

A coisa equivalente com awkseria lida

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

ou, mais curto,

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

informação relacionada