I want to programmatically edit file content using windows command line (cmd.exe). In *nix there is sed for this tasks. Are there any useful native equivalents (cmd or ps) in windows?
-
1What do you mean by "native" ??? Installable to work in cmd.exe, or to work without installation whatsoever? If the former, see GnuWin32 ref'd below; if the latter, no. No pre-installed native sed for windows. – Michael Paulukonis May 26 '09 at 13:10
-
9By native I meant solution which runs on all windows without installing additional stuff. – Jakub Šturc May 26 '09 at 17:34
-
2Leave cmd.exe behind and use PowerShell instead. – Bill_Stewart Jan 01 '15 at 14:59
-
Related: http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – AlikElzin-kilaka Nov 26 '15 at 16:11
-
If you just want to delete certain lines from a file, use FIND /? – John Henckel Apr 25 '16 at 13:04
-
If you just want to search and replace text in a string or variable in CMD, you can just do `%var:old=new%`. See https://ss64.com/nt/syntax-replace.html for more info. – wisbucky Mar 27 '18 at 23:31
16 Answers
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.

- 317
- 2
- 10

- 35,201
- 25
- 90
- 110
-
19In addition: if you want to call it from normal cmd, just @powershell -Command "get-content..." it. The only caveat is that you must escape quotations marks: ... -Command "get-content ... \"expression\",..." – Tarc Jul 25 '14 at 19:13
-
1@Tarc AFAIR, you can use single quotes in your command and use double quotes to quote the command instead – Apr 13 '16 at 16:15
-
3I tried this today on a file to remove some stuff between quotes in certain places, only to discover that the output was a file almost twice as large as the original. The reason appeared to be that this method changed the way newlines were handled in the file to use "\r\n" instead of "\n". – user132278 Sep 26 '16 at 01:16
-
2Doubling due to \r\n would imply that your file was composed purely of newlines. Here is the real reason: [encoding](http://stackoverflow.com/questions/21374118/size-of-the-sorted-file-is-double-than-original-file-in-powershell). – Amit Naidu Oct 08 '16 at 01:20
-
1@Jakub Šturc : is there a easy way to do this `sed -i 's/foo/bar/g' *` – and-bri Apr 30 '18 at 15:49
-
4@and-bri (Get-Content c:\temp\test.txt).replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt – AFP_555 May 03 '19 at 15:44
-
1instead of `get-content | where` why don't just use [`select-string`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-6)? – phuclv Nov 28 '19 at 11:03
-
-
2@JakubŠturc The solution you put for replacing `sed` didn't work for me. I used this instead and it worked fine: `(Get-Content myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt` (this is for replacing the text 'foo' with the text 'bar' in the entire file. This was taken from https://stackoverflow.com/a/20999154/4441211 – Eyal Gerber Dec 08 '21 at 09:13
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.
- Windows Subsystem for Linux is a most "native" option, but it's not installed on Windows by default; it has
sed
,grep
etc. out of the box, though. - https://github.com/mbuilov/sed-windows offers recent 4.3 and 4.4 versions, which support
-z
option unlike listed upper ports
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
-
1This is not exactly what I want however I beleave that the dependency upon VBS is the most lightest solution. – Jakub Šturc Sep 24 '08 at 16:08
-
-
7GnuWin32 _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 May 26 '09 at 13:09
-
3
-
4If this is "off-the-cuff", wonder what your other code looks like! Amazing, thanks. – Sabuncu Jul 14 '11 at 07:10
-
1@dbenham You're adding links to your own scripts in others' answers, and phrasing it as if to show support for it? How is that anything other than spam? – Sep 25 '13 at 17:08
-
@hvd - I don't normally add content to other's answers. However, I think the added info deserves to be more than a comment, but isn't worthy of a stand-alone answer to this question. The linked utility is an axtension of the VBS ideas already presented in this answer, but implemented as JScript instead of VBS. It is an example of how the idea could be "fleshed out", so I certainly don't consider it spam. But I'll admit, I did consider some might object, so this was a bit of an experiment. If the OP or general community objects, I won't mind if it is rolled back. – dbenham Sep 25 '13 at 18:01
-
@dbenham - my take on refinements or derivative answers is that they should be comments, and let them get upvoted to the moon. I can accept that you feel it's an "update," but it feels a bit like you hijacked my answer. That said, your code is interesting and i'll take a look at it. So, thanks i guess. :) – b w Sep 26 '13 at 14:42
-
1@hvd - Based on Bill's understandable sentiment, I've reconsidered, and rolled back my edit. That was a failed experiment, so I won't be doing this again :) I only wish an edit rollback were a true rollback. – dbenham Sep 26 '13 at 15:53
-
2A similar regex search and replace utility can be done with JScript. The advantage of JScript over VBScript is it can be easily combined with batch in a single script, making the utility easier to use in a batch or command line context. [A robust hybrid JScript/batch utility called REPL.BAT is available](http://stackoverflow.com/a/16735079/1012053). It has many command line options. Full documentation is embedded within the script. It certainly doesn't have full sed capability, but even so, it is quite powerfull. – dbenham Sep 26 '13 at 15:54
-
@dbenham - you're a gentleman and a scholar. +1 and +1. I **do** like your jscript solution, though i'm going to have to take some time to digest it. Very interesting. – b w Sep 26 '13 at 17:41
-
Don't work if you have binary content. Works perfectly with sed in GnuWin32. – JoeBilly Apr 02 '14 at 08:53
-
@JoeBilly - True, but OP asks for a native command line solution. GnuWin32 sed is not. – b w Apr 02 '14 at 15:57
-
Chocolatey has had this for a while now: https://chocolatey.org/packages/sed which is the GNU sed and includes the `-z` / `--null-data` option. I just checked; currently it installs `sed.exe` which is binary the same as https://github.com/mbuilov/sed-windows/blob/cafe68124fb8f01db3fb1d9ea586f8f6a72d6917/sed-4.5-x64.exe – Jeroen Wiert Pluimers Jul 17 '19 at 08:58
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.
> (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

- 171
- 1
- 4
-
3It works perfectly. Note: the parenthesis on "get-content" ensure that the file will not be in use later when "set-content" will be called. – Kafumanto May 24 '17 at 17:48
-
1is something similar possible in cmd? in understand this is for pwsh only – Ferroao Jan 09 '20 at 11:27
-
is 'SourceRegexp' ment to be substituded by any possible regex? does that really works in power shell? – scjorge Feb 27 '20 at 16:40
Try fart.exe. It's a Find-and-replace-text utility that can be used in command batch programs.

- 329
- 2
- 2
-
If I really can't use a *nix like shell, I prefer this since it's a small standalone exe with no dependencies. – Qben Oct 01 '12 at 12:32
-
You could try powershell. There are get-content and set-content commandlets build in that you could use.

- 41,623
- 1
- 17
- 5
-
4Those are probably the two things in PowerShell that contribute least to what `sed` does ;-). The `-replace` operator is probably a better suggestion. – Joey Apr 21 '10 at 08:51
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).

- 4,810
- 5
- 25
- 31
You could install Cygwin (http://www.cygwin.com/) and use sed from there.

- 27,571
- 51
- 138
- 205
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

- 4,522
- 4
- 40
- 45
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

- 20,879
- 9
- 40
- 61

- 40,353
- 10
- 53
- 68
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.

- 41,768
- 14
- 66
- 83
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.

- 6,969
- 5
- 43
- 64
-
1
-
@royhowie: I see. Although some of these answers may be useful to somebody, I agree that the question itself isn't really suitable for SO. OTOH, I don't feel qualified to recommend wholesale deletion. – PM 2Ring Jan 01 '15 at 09:30
-
In this situation I think it would be difficult to include the essential parts of the answer, since the essential part is an .exe file that is hosted on the linked page. I totally understand the worry about link-rot, I'm just unsure how I could make this self-contained to SO. – bryan kennedy Jan 01 '15 at 14:52
-
Thanks for the catch, a_horse_with_no_name. I was being sloppy with my language. I edited my comment to fix the problem you pointed out. – bryan kennedy Jan 01 '15 at 14:53