Is there any sed like utility for cmd.exe?

WindowsSedCmd

Windows Problem Overview


I want to programmatically edit file content using windows command line (cmd.exe). In *nix there is sed for this tasks. Is there any useful native equivalent in windows?

Windows Solutions


Solution 1 - Windows

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.

Solution 2 - Windows

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

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

Solution 3 - Windows

UnxUtils provides sed for Win32, as does GNUWin32.

Solution 4 - Windows

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.

Solution 5 - Windows

> (Get-content file.txt) | Foreach-Object {$_ -replace "^SourceRegexp$", "DestinationString"} | Set-Content file.txt

This is behaviour of

sed -i 's/^SourceRegexp$/DestinationString/g' file.txt

Solution 6 - Windows

There is Super Sed an enhanced version of sed. For Windows this is a standalone .exe, intended for running from the command line.

Solution 7 - Windows

You could try powershell. There are get-content and set-content commandlets build in that you could use.

Solution 8 - Windows

I use Cygwin. I run into a lot of people that do not realize that if you put the Cygwin binaries on your PATH, you can use them from within the Windows Command shell. You do not have to run Cygwin's Bash.

You might also look into Windows Services for Unix available from Microsoft (but only on the Professional and above versions of Windows).

Solution 9 - Windows

Try fart.exe. It's a Find-and-replace-text utility that can be used in command batch programs.

http://sourceforge.net/projects/fart-it/

Solution 10 - Windows

You could install Cygwin (http://www.cygwin.com/) and use sed from there.

Solution 11 - Windows

edlin or edit

plus there is Windows Services for Unix which comes with many unix tools for windows. http://technet.microsoft.com/en-us/interopmigration/bb380242.aspx

Update 12/7/12 In Windows 2003 R2, Windows 7 & Server 2008, etc. the above is replaced by the Subsystem for UNIX-Based Applications (SUA) as an add-on. But you have to download the utilities: http://www.microsoft.com/en-us/download/details.aspx?id=2391

Solution 12 - Windows

There is a helper batch file for Windows called repl.bat which has much of the ability of SED but doesn't require any additional download or installation. It is a hybrid batch file that uses Jscript to implement the features and so is swift, and doesn't suffer from the usual poison characters of batch processing and handles blank lines with ease.

Download repl from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Alternative link - https://www.dostips.com/forum/viewtopic.php?f=3&t=6044

The author is @dbenham from stack overflow and dostips.com

Another helper batch file called findrepl.bat gives the Windows user much of the capabilty of GREP and is also based on Jscript and is likewise a hybrid batch file. It shares the benefits of repl.bat

Download findrepl from - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

The author is @aacini from stack overflow and dostips.com

Solution 13 - Windows

You could look at GNU Tools, they provide (amongst other things) sed on windows.

Solution 14 - Windows

As far as I know nothing like sed is bundled with windows. However, sed is available for Windows in several different forms, including as part of Cygwin, if you want a full POSIX subsystem, or as a Win32 native executable if you want to run just sed on the command line.

Sed for Windows (GnuWin32 Project)

If it needs to be native to Windows then the only other thing I can suggest would be to use a scripting language supported by Windows without add-ons, such as VBScript.

Solution 15 - Windows

Cygwin works, but these utilities are also available. Just plop them on your drive, put the directory into your path, and you have many of your friendly unix utilities. Lighterweight IMHO that Cygwin (although that works just as well).

Solution 16 - Windows

I needed a sed tool that worked for the Windows cmd.exe prompt. Eric Pement's port of sed to a single DOS .exe worked great for me.

It's pretty well documented.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakub ŠturcView Question on Stackoverflow
Solution 1 - WindowsJakub ŠturcView Answer on Stackoverflow
Solution 2 - Windowsb wView Answer on Stackoverflow
Solution 3 - WindowsAdam HughesView Answer on Stackoverflow
Solution 4 - WindowsRoberView Answer on Stackoverflow
Solution 5 - WindowskrogonView Answer on Stackoverflow
Solution 6 - WindowsRob KamView Answer on Stackoverflow
Solution 7 - WindowsJames BootherView Answer on Stackoverflow
Solution 8 - WindowsBrandon DuRetteView Answer on Stackoverflow
Solution 9 - WindowsColin NichollsView Answer on Stackoverflow
Solution 10 - WindowsrobintwView Answer on Stackoverflow
Solution 11 - WindowsBooji BoyView Answer on Stackoverflow
Solution 12 - WindowsfoxidriveView Answer on Stackoverflow
Solution 13 - WindowsLinorView Answer on Stackoverflow
Solution 14 - WindowsJayView Answer on Stackoverflow
Solution 15 - WindowsMarkView Answer on Stackoverflow
Solution 16 - Windowsbryan kennedyView Answer on Stackoverflow