0

Im using below to append proxyport=8080 to the end of the line where javaagent.jar string found and below is working fine, but this should ignore if there is a # (commented line) in the text file

sed -i '' -e '/javaagent.jar/ s/$/ proxyPort=8080/' 

Example input:

#/opt/ver/inmind/javaagent.jar
/opt/ver/inmind/javaagent.jar

Example output should be:

#/opt/ver/inmind/javaagent.jar
/opt/ver/inmind/javaagent.jar proxyPort=8080
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
CSR
  • 39
  • 3

1 Answers1

1

Assuming you have a FreeBSD sed version (as you have -i '' in your command), you can use

sed -i '' -e '/^#/!s/javaagent\.jar.*/& proxyPort=8080/' file

See the online demo.

The /^#/! part stops processing lines starting with # (add [[:space:]]* after ^ to account for indentation, any leading whitespace), and if there is no # at the start, s/javaagent\.jar.*/& proxyPort=8080/ finds javaagent.jar + the rest of the line and replaces this match with itself (with &) and adds a space and proxyPort=8080.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563