Issue
I am having a file named test.js.
the content of the test.js can be dynamic . It can be the following.
export default function () {
http.get("http://@@NODE_IP@@:@@NODE_PORT@@/SccTestService/v1/id");
}
or it can be as below . (The ip address and port can change.)
export default function () {
http.get("http://172.19.32.1:30331/SccTestService/v1/id");
}
In my program i receive a test.js file. i want to replace the ip address and port with another ip address and port. How can I achieve this . Basically I want to combine the following scenario.
IP=172.15.33.101
HTTP_PORT=32231
scenario 1.
sed -i -e "s/@@NODE_IP@@/$IP/g" test.js
sed -i -e "s/@@NODE_PORT@@/$HTTP_PORT/g" test.js
scenario 2 .( Note the IP address and port can change. In this example i am using 172.19.32.1 and 30331. )
sed -i -e "s/172.19.32.1/$IP/g" test.js
sed -i -e "s/30331/$HTTP_PORT/g" test.js
How can I, use generic sed command to handle all cases ?
In short the
sed should be able to replace @@NODE_IP@@
or any other provided IP address in the file .
Also another generic sed command to replace the @@NODE_PORT@@
or any other port number with a provided one . The solution should aims to use regex preferably
appreciate your help thank you
Solution
I would use extended regular expressions (-E) in your case:
sed -i -E -e "s@//[0-9]+(\.[0-9]+){3}@//$IP@" -e "s@:[0-9]{2,5}/@:$HTTP_PORT/@" test.js
Let's decompose the command just in case:
sed -i -E
tellssed
to edit the file and use extended regex.-e "s@//[0-9]+(\.[0-9]+){3}@//$IP@"
looks for two slashed followed by a number with any amount of digits, followed by a group (repeated 3 times) composed of a dot followed by a number with any amount of digits. Technically, that would also match//7658.0214.1245147.124369
but it shouldn't be an issue here.-e "s@:[0-9]{2,5}/@:$HTTP_PORT/@"
looks for a number having between 2 and 5 digits, between two dots and a slash. This should avoid false positives in case your URL itself is subject to change, matching only a group of numbers would also match parts of the IP address.
I used @
as the separator to avoid escaping slashes. I also avoided adding back-references and used two expressions because that matches your tests, but it could be put together into a single expression with the added benefit of being sure you're matching an IP address followed by a port.
Here's what the command to match an already defined IP and port would look like:
sed -i -E -e "s@//[0-9]+(\.[0-9]+){3}:[0-9]+/@//$IP:$HTTP_PORT/@" test.js
I removed the strict checking on the amount of digits for the port, but kept the leading slashes in case you ever call an API that takes an IP and a port as a path parameter.
Finally, we can add your scenario #1 to the command as a second expression, which doesn't need to be as strict since you use what appears to be a pattern. We don't have to separate IP and port either since they're supposed to be together.
Here's what I believe is the final command:
sed -i -E -e "s@//[0-9]+(\.[0-9]+){3}:[0-9]+/@//$IP:$HTTP_PORT/@" -e "s/@@NODE_IP@@:@@NODE_PORT@@/$IP:$HTTP_PORT/" test.js
Hope it helps, don't hesitate if you need any clarification.
Answered By - Xenthys Answer Checked By - Marilyn (WPSolving Volunteer)