Is there any sed like utility for cmd.exe?

Today powershell saved me.

For grep there is:

get-content somefile.txt | where { $_ -match "expression"}

or

select-string somefile.txt -pattern "expression"

and for sed there is:

get-content somefile.txt | %{$_ -replace "expression","replace"}

For more detail about replace PowerShell function see this Microsoft article.


sed (and its ilk) are contained within several packages of Unix commands.

  • Cygwin works but is gigantic.
  • UnxUtils is much slimmer.
  • GnuWin32 is another port that works.
  • Another alternative is AT&T Research's UWIN system.
  • MSYS from MinGw is yet another option.
  • Windows Subsystem for Linux is a most "native" option, but it's not installed on Windows by default; it has sed, grep etc. out of the box, though.
  • https://github.com/mbuilov/sed-windows offers recent 4.3 and 4.4 versions, which support -z option unlike listed upper ports

If you don't want to install anything and your system ain't a Windows Server one, then you could use a scripting language (VBScript e.g.) for that. Below is a gross, off-the-cuff stab at it. Your command line would look like

cscript //NoLogo sed.vbs s/(oldpat)/(newpat)/ < inpfile.txt > outfile.txt

where oldpat and newpat are Microsoft vbscript regex patterns. Obviously I've only implemented the substitute command and assumed some things, but you could flesh it out to be smarter and understand more of the sed command-line.

Dim pat, patparts, rxp, inp
pat = WScript.Arguments(0)
patparts = Split(pat,"/")
Set rxp = new RegExp
rxp.Global = True
rxp.Multiline = False
rxp.Pattern = patparts(1)
Do While Not WScript.StdIn.AtEndOfStream
  inp = WScript.StdIn.ReadLine()
  WScript.Echo rxp.Replace(inp, patparts(2))
Loop

Tags:

Windows

Cmd

Sed