1

I'm trying to run a sed command on a Windows machine from the command prompt (CMD.exe) but I am struggling to understand the regular expression and how to escape the string properly when running on Windows. Ideally, I want to develop a solution that works across UNIX and Windows.

sed is not available on Windows so I have installed it via the gnuWin32 project which works well.

The unix format for the command is:

sed -i '' -e 's/\\/_next/\\.\\/next/g' out/**.html

Through a process of trial an error I have managed to get this far:

sed -i \'\' -e \'s/\\/_next/\\.\\/next/g\'  out/**.html

but I get an error: sed: -e expression #1, char 27: unterminated address regex

So there's definitely something wrong with my regex, probably the escaping of various parts? Any ideas how I might go about fixing this?

Update:

I'm getting the code from here where unfortunately only Linux and OSX are covered.

phuclv
  • 37,963
  • 15
  • 156
  • 475
James Mundy
  • 4,180
  • 6
  • 35
  • 58
  • Try `sed -i "s/\\/_next/\\.\\/next/g" out/**.html` – Wiktor Stribiżew Nov 23 '22 at 16:49
  • @WiktorStribiżew thanks very much this worked! Really appreciate the assistance. If you'd like to write it as answer and maybe a tiny bit about where I went wrong I can accept? – James Mundy Nov 23 '22 at 17:06
  • Avoid cmd. It has quirky escaping rules due to legacy issues. The escape character is `^`, not `` \ ``, but sometimes a backslash is required. It also has other nasty things that don't exists in other shells. Use powershell instead which is much more powerful. You don't need sed at all: [PowerShell Script to Find and Replace for all Files with a Specific Extension](https://stackoverflow.com/q/2837785/995714) – phuclv Nov 23 '22 at 17:19

1 Answers1

2

You need to use

sed -i "s/\\/_next/\\.\\/next/g" out/**.html

The Windows GNU sed does not require the '' empty argument after -i option, they can be safely removed.

Also, the sed command in Windows console should be used in double quotes.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    there's no such thing as "Windows console". Console or terminal is the thing that displays output and is what a shell like cmd, bash, powershell, ksh... attaches to. In cmd single quote is not a special symbol but in powershell it is – phuclv Nov 23 '22 at 17:14
  • @phuclv I used it to call the command prompt `cmd` window. – Wiktor Stribiżew Nov 23 '22 at 17:34