0

I have a json file(config.json) with the contents as shown below.

{
 "context" : {
 "inprocAddress" : "inproc://bigpc",
  "uniqueIdentity" : 3,
 "name" : "ccontroller"
 },
   "controllerListenerPort" : 3051,
   "controllerServerHandshakePort" : 3052,
   "controllerServerInprocAddress" : "inproc://controllerdeal",
   "controllerServerRouterPort" : 3054,
   "controllerServerNumWorkers" : 44,
   "controllerNumContextThreads" : 1,
   "mongoConnectionPoolSize" : 44,
   "fileSystemRootDirectory" : "C:/Controller/ControllerData",
   "mongoURI" : "mongodb:desktop-stl.mo.mywebsite.com:27017/"
} 

I want to replace the value of mongoURI tag to mongodb:localhost:27017/. I tried using the following script, but I'm unable to setup the regex part.

setlocal enableextensions disabledelayedexpansion

set "search="mongodb"
set "replace="mongodb://localhost:12707""

set "textFile=C:\Users\shravan\Documents\Rahul\config.json"

for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    >>"%textFile%" echo(!line:%search%=%replace%!
    endlocal
)

With this script, I get the following as the replaced text at mongoURI:

"mongoURI" : "mongodb://localhost:12707":desktop-cblrrri.stl.mo.boeing.com:27017/"

Rahul
  • 986
  • 2
  • 11
  • 25
  • 1
    It only replaces the specific part you tell it to, it does not replace the line. If you tell it to replace `Bob` in `Bob Thornton` to `Pete` you'll get `Pete Thornton`. Have a look at [this Answer](https://stackoverflow.com/questions/63036816/batch-file-to-add-git-branch-into-header-file/63043138#63043138) how I did something similar, but matching the line and replace the entire line. – Gerhard Jul 27 '20 at 17:35
  • 1
    Batch scripts are not designed for searching and replacing text, and certainly not in `.json` files, which can have non windows encoding and line endings. I'd strongly suggest you use [tag:powershell] instead, which has built-in support for `.json`, has considerably better regex implementation, and can replace directly instead of reading, writing to a holding file and then replacing the content of the original with that of the holding file. – Compo Jul 27 '20 at 17:39
  • 1
    so, on my answer, you should be able to replace the `if` line to `if "!line:~4,8!" == "mongoURI" set "line= "mongoURI" : mongodb://localhost:12707";"` – Gerhard Jul 27 '20 at 17:56
  • Thanks for the answer. It works to the given indentation. Can you please give me a more generic solution, instead of using 4th character to start of the "mongoURI". As it may not be the case some times due to indentation. – Rahul Jul 27 '20 at 18:04
  • 1
    That is the problem with batch, you have to hack bits and pieces to get it to work, I will see what I can do, but for the best results, you'll need to use powershell if you want a robust solutionn. – Gerhard Jul 27 '20 at 18:10

1 Answers1

2

As a supplement to my comment, here's a simple + example:

$f = "$Env:UserProfile\Documents\Rahul\sample.json"
$u = "mongodb:localhost:27017/"
$a = Get-Content $f -Raw | ConvertFrom-Json
$a.mongoURI = $u
$a | ConvertTo-Json | Set-Content $f

Change your target filename on line 1, and your new URI content on line 2.

Compo
  • 30,301
  • 4
  • 20
  • 32