0

this is the file that we want to delete all lines from the line that include “EXPORTER_JAR_PATH” until end of file

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

this is my solution to delete the lines from line that include - EXPORTER_JAR_PATH until end

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

As we can see the line - Export EXPORTER_JAR_PATH=/tmp/hgt.yml

Still exists , where we are wrong ?

Expected output

# 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
0
0
sed '/EXPORTER_JAR_PATH/,$d' file

This would delete all lines from the first line that contains the substring EXPORTER_JAR_PATH to the end of the file ($ addresses the end of the file), inclusively.

What your command does is to delete all lines that does not fall within the range between line 1 and the line that contains that string, inclusively. This means that the line that contains the substring EXPORTER_JAR_PATH would not be deleted.

Alternatively, as pointed out in comments below by both Paul_Pendant and mosvy,

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

which would explicitly, with p, print out each line until it reaches the EXPORTER_JAR_PATH line where the script would terminate. The line with the string wouldn't get printed due to the -n option disabling the ordinary default output. The benefit of this is that sed would not have to read the whole file (it would however not make a drastic difference in this specific case as the file is so short).

The equivalent thing with awk would read

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

or, shorter,

awk '/EXPORTER_JAR_PATH/ { exit }; 1' file
5
  • sed '/EXPORTER_JAR_PATH/Q' file does the same, but is a whole lot faster if the trigger is early in the file: it avoids reading the remainder of the file. – Paul_Pedant Apr 2 '20 at 18:03
  • @Paul_Pedant I suppose Q is a GNU sed instruction, right? – Kusalananda Apr 2 '20 at 18:09
  • The Q is Gnu extension, because q outputs the pattern space first. Equivalent Posix is sed -n '/calcChain/q; p' – Paul_Pedant Apr 2 '20 at 18:15
  • @Paul_Pedant no need for any GNU exension: sed -n '/EXPORTER_JAR_PATH/q;p' – mosvy Apr 2 '20 at 22:02
  • @mosvy Showed the non-GNU syntax, but the trigger pattern is from my 23MB XML test file. Assumed the exact application is obvious. Timed at 0.148 secs with ,$d and 0.008 with q;p. – Paul_Pedant Apr 3 '20 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.