一致する行を含む、一致する行から最後まですべての行を削除します。

一致する行を含む、一致する行から最後まですべての行を削除します。

これは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

おすすめ記事