views:

7350

answers:

14

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.

+1  A: 

You could use cygwin?

JeeBee
+5  A: 

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

robintw
+4  A: 

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

Booji Boy
Both don't exist anymore on 64-bit versions of Windows.
Joey
+2  A: 

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

Linor
+7  A: 

UnxUtils provides sed for Win32, as does GNUWin32.

Adam Hughes
+1  A: 

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

Mark
+1  A: 

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.

Jay
+4  A: 

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

James Boother
Those are probably the two things in PowerShell that contribute least to what `sed` does ;-). The `-replace` operator is probably a better suggestion.
Joey
+3  A: 

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

Brandon DuRette
+9  A: 

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.

EDIT after re-reading "native" several times...

Okay, you could build something sed-like in vbscript. Below is a gross, off-the-cuff stab at it. Your command line would look like cscript 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
bill weaver
This is not exactly what I want however I beleave that the dependency upon VBS is the most lightest solution.
Jakub Šturc
Anyway thank you a lot and all others for answers.
Jakub Šturc
GnuWin32 _is_ a native solution, if by native you mean designed to run in the "normal" windows environment (unlike CygWin). Give them a try, and you'll start expecting them to be on EVERY windows system you use. !!!
Michael Paulukonis
Works for my case! Also, don't forget "//NoLogo" cscript option.
Marko Dumic
+1  A: 

This works on Vista Ultimate, not sure Pro.

sed -f commandfilename.cmd file1 > file2

+1  A: 

simply said you are talking about Perl or Python or even Parrot. It will take just time to realize it ..

YordanGeorgiev
A: 

MKS Toolkit provides a good list of *nix style utilities.

www.mkssoftware.com/

Raj More
+1  A: 

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

Rob Kam