Question

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

Edit: I am looking for native command line solution.

Was it helpful?

Solution

Today powershell saved me.

For grep there is:

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

and for sed there is:

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

For more detail see Zain Naboulsis blog entry.

OTHER TIPS

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

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.

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

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/

> (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

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

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

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).

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

You could look at GNU Tools, they provide (amongst other things) sed on 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

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

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.

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).

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.

This works on Vista Ultimate, not sure about Pro.

sed -f commandfilename.cmd file1 > file2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top