0

I prepared a command that works and does its job correctly

powershell -Command "(gc copy.json) -replace 'field_type','field_name' -replace 'aaa','AAA' -replace 'Bbb','BBB' | Out-File copy12345.json"

But I would like each '-replace ...' to be on a new line for better readability. I tried the following solution and also brackets for the whole command, but it didn't help.

powershell -Command "(gc copy.json) ^
-replace 'field_type', 'field_name' ^
-replace 'aaa','AAA' ^
-replace 'Bbb','BBB' ^
| Out-File kopia12345.json"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
Luk1919
  • 11
  • 1
    it is inside double quotes, meaning the line is passed to `powershell` which uses backticks. but now you're sitting with a problem where cmd does not understand what you want. Why not run this directly from `powershell` instead? Anyway, leave the carets as is and remove the outer double quotes and it should work. – Gerhard Oct 17 '22 at 10:26
  • 1
    Gerhard is correct. The linked duplicate discusses all requirements in detail, notably not being able to use `"..."` for overall quoting (as noted) and needing to `^`-escape `|` too. – mklement0 Oct 17 '22 at 11:58

1 Answers1

0
powershell -Command "(gc copy.json) `
-replace 'field_type','field_name' `
-replace 'aaa','AAA' `
-replace 'Bbb','BBB' `
| Out-File copy12345.json"

reference: https://shellgeek.com/powershell-multiline-command/#:~:text=To%20split%20long%20command%20into,split%20it%20into%20multiple%20lines.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Addy
  • 1
  • 1
  • 1
    This will not work. sorry, it is being launched from `cmd` and `cmd` does not know what ` is – Gerhard Oct 17 '22 at 10:39
  • https://stackoverflow.com/questions/2768608/batch-equivalent-of-bash-backticks may be this can help you – Addy Oct 17 '22 at 11:38
  • Sorry, Addy, but still not relevant. See my comment to the answer and also [this link](https://stackoverflow.com/questions/45224759/using-multi-line-powershell-commands-from-cmd-exe) – Gerhard Oct 17 '22 at 12:05