Is there any sed like utility for cmd.exe? [closed

2018-12-31 22:21发布

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.

标签: windows sed cmd
17条回答
琉璃瓶的回忆
2楼-- · 2018-12-31 22:44

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.

查看更多
梦寄多情
3楼-- · 2018-12-31 22:44

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

查看更多
高级女魔头
4楼-- · 2018-12-31 22:45

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

查看更多
像晚风撩人
5楼-- · 2018-12-31 22:47

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

查看更多
浮光初槿花落
6楼-- · 2018-12-31 22:47

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.

查看更多
登录 后发表回答