we want to add the following lines before the match word - exec
in launcher file
export JAVA_HOME=/usr/jdk64/jdk1.8.0_112
export PATH=$PATH:$JAVA_HOME/bin
the launcher file
more launcher
.
.
.
exec "$(dirname "$0")/launcher.py" "$@"
so we create the following sed line
sed -i '/^exec/i export JAVA_HOME=/usr/jdk64/jdk1.8.0_112\nPATH=${JAVA_HOME}/bin:$PATH' launcher
the problem is that the second or the third time when we run the above sed line then sed append anew lines , so we get duplicate lines as
export JAVA_HOME=/usr/jdk64/jdk1.8.0_112
PATH=${JAVA_HOME}/bin:$PATH
export JAVA_HOME=/usr/jdk64/jdk1.8.0_112
PATH=${JAVA_HOME}/bin:$PATH
exec "$(dirname "$0")/launcher.py" "$@"
is it possible to tell sed to ignore appending when lines already exists? , ( or with perl one liner )
one option is to clean the lines with:
sed -i '/JAVA_HOME/d' launcher
and then
sed -i '/^exec/i export JAVA_HOME=/usr/jdk64/jdk1.8.0_112\nPATH=${JAVA_HOME}/bin:$PATH' launcher
but want to know also if we can do it inside the above sed line