Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any sed like utility for cmd.exe? [closed]

Tags:

windows

sed

cmd

People also ask

Is there a SED for Windows?

You can download a copy of SED for Windows from SourceForge. I selected and installed the “Complete package, except sources” to the default location in Program Files. I then spent a while looking through my book on SED & AWK and various web sites to try and get together the pieces for the script that I wanted to run.

Is cmd.exe deprecated?

The Windows Cmd / Command-Line shell is NOT being removed from Windows in the near or distant future! The Cmd shell remains an essential part of Windows, and is used daily by millions of businesses, developers, and IT Pro's around the world.

Is shell like cmd?

The command-line shell, sometimes called the command prompt or the terminal, is a tool that lets you control your computer using only textual commands. It offers a lot of power and simplicity (simplicity is different from ease of use).


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

UnxUtils provides sed for Win32, as does GNUWin32.


If you don't want to install anything (I assume you want to add the script into some solution/program/etc that will be run in other machines), you could try creating a vbs script (lets say, replace.vbs):

Const ForReading = 1
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText
objFile.Close

And you run it like this:

cscript replace.vbs "C:\One.txt" "Robert" "Rob"

Which is similar to the sed version provided by "bill weaver", but I think this one is more friendly in terms of special (' > < / ) characters.

Btw, I didn't write this, but I can't recall where I got it from.