How to add a new line to a shell file above a specific line with sed

383 Views Asked by At

I want to add a new line for the file apache-jmeter 4.0/bin/jmeter

The last 2 lines of the file is as follows

 fi
 "$JAVA_HOME/bin/java" $ARGS $JVM_ARGS $JMETER_OPTS -jar. "$PRGDIR/ApacheJMeter.jar" "$@" 

I want to add a line above the last line of the file to make the output to be like as below

 fi
 #JVM_ARGS="-Xbootclasspath/p:/home/ubuntu/alpnboot.jar"
"$JAVA_HOME/bin/java" $ARGS $JVM_ARGS $JMETER_OPTS -jar. "$PRGDIR/ApacheJMeter.jar" "$@"
2

There are 2 best solutions below

0
On

The relevant sed command would be something like:

sed 's/^"\$JAVA_HOME\/bin\/java" \$ARGS \$JVM_ARGS \$JMETER_OPTS -jar "\$PRGDIR\/ApacheJMeter\.jar" "\$@"$/#JVM_ARGS="-Xbootclasspath\/p:\/home\/ubuntu\/alpnboot\.jar"\n&/' jmeter

Remember that sed uses Posix Basic Regular Expressions therefore you will need to escape any of $.*/[\]^ characters with a backslash.


If you're going to use HTTP2 Sampler you can make the change permanent, this -Xbootclasspath option will make no harm, there is not much sense in turning it on and off.

0
On

In Running JMeter it's suggested to add such option to setenv.sh file:

To set those variables permanently, you can place them in a file called setenv.sh in the bin directory. This file will be sourced when running JMeter by calling the jmeter script.