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?
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?
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
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
> (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
There is Super Sed an enhanced version of sed. For Windows this is a standalone .exe, intended for running from the command line.
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).
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
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
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.