Reputation: 4941
My shell script looks like this:
FAILED_REQUEST_DIRECTORY=/bla/bla2 java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234 -jar /xyz/abc-service-1.0-SNAPSHOT.jar --server.port=1111 --server.address=127.0.0.1 --kafka.bootstrap.servers=10.111.11.11:3197,10.112.12.111:3197,10.123.44.25:3197 --topic.pushcrew.encryptionKey=abc-def-egh --spring.application.name=name-sevice --fb.verification.token=token --management.trace.include.payload=true --topic.pushcrew.hits=activity-logs --max.block.ms=1000 --pager.duty.api.key=apikey --dsn=dsn
If I have to replace values of keys in the command, for ex: replacing value of key --server.address
with the value to shell script through a variable, how would I do it?
I understand it would look like following if I had to replace value for key --server.address:
sed -i "s#--server.address=.* #$SERVER_ADDRESS#g;
But how do I maintain the space that has to be there after the replacement and how do I make sure that part int the key only after =
(equals to) and before (space) gets replaced.
Upvotes: 1
Views: 92
Reputation: 133428
If you are ok with awk
, could you please try following.(to save output into Input_file itself append > temp_file && mv temp_file Input_file
to following code)
val='10.0.0.1'
awk -v var="$val" '
match($0,/--server\.address=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){
val=substr($0,RSTART,RLENGTH)
sub(/=.*/,"="var,val)
print substr($0,1,RSTART-1) val substr($0,RSTART+RLENGTH)
next
}
1
' Input_file
Output will be as follows.
FAILED_REQUEST_DIRECTORY=/bla/bla2 java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234 -jar /xyz/abc-service-1.0-SNAPSHOT.jar --server.port=1111 --server.address=10.0.0.1 --kafka.bootstrap.servers=10.111.11.11:3197,10.112.12.111:3197,10.123.44.25:3197 --topic.pushcrew.encryptionKey=abc-def-egh --spring.application.name=name-sevice --fb.verification.token=token --management.trace.include.payload=true --topic.pushcrew.hits=activity-logs --max.block.ms=1000 --pager.duty.api.key=apikey --dsn=dsn
Upvotes: 1
Reputation: 784918
You may use this sed
with 2 capturing groups and back-references:
val='10.0.0.1'
sed -E "s/^(.*--server.address=)[^[:blank:]]+(.*)$/\1$val\2/" file.properties
RegEx:
^(.*--server.address=)
: Match everything at start till --server.address=
substring and capture in group #1[^[:blank:]]*
: Match 0 or more non-space characters(.*)$
: Match everything till and end and capture in group #2Replacement:
\1
: Back-reference to capture group #1$val
: Place desired $val
in replacement
FAILED_REQUEST_DIRECTORY=/bla/bla2 java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234 -jar /xyz/abc-service-1.0-SNAPSHOT.jar --server.port=1111 --server.address=10.0.0.1 --kafka.bootstrap.servers=10.111.11.11:3197,10.112.12.111:3197,10.123.44.25:3197 --topic.pushcrew.encryptionKey=abc-def-egh --spring.application.name=name-sevice --fb.verification.token=token --management.trace.include.payload=true --topic.pushcrew.hits=activity-logs --max.block.ms=1000 --pager.duty.api.key=apikey --dsn=dsn
Upvotes: 1