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.
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
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
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
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.
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
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
This works on Vista Ultimate, not sure Pro.
sed -f commandfilename.cmd file1 > file2
This is behaviour of
sed
(and its ilk) are contained within several packages of Unix commands.sed
,grep
etc. out of the box, though.-z
option unlike listed upper portsIf 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
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.Today powershell saved me.
For
grep
there is:and for
sed
there is:For more detail see Zain Naboulsis blog entry.
There is a helper batch file for Windows called
repl.bat
which has much of the ability of SED butdoesn't require any additional download
or installation. It is a hybrid batch file that usesJscript
to implement the features and so isswift
, anddoesn'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.batThe author is @dbenham from stack overflow and dostips.com
Another helper batch file called
findrepl.bat
gives the Windows user much of the capabilty ofGREP
and is also based onJscript
and is likewise a hybrid batch file. It shares the benefits of repl.batDownload
findrepl
from - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.batThe author is @aacini from stack overflow and dostips.com