
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