это файл, в котором мы хотим удалить все строки, начиная с строки, содержащей « 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 и строкой, содержащей эту строку, включительно. Это означает, что строка, содержащая подстроку, 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