首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否有用于cmd.exe的类似sed的实用程序?

是否有用于cmd.exe的类似sed的实用程序?
EN

Stack Overflow用户
提问于 2008-09-24 14:01:42
回答 14查看 259K关注 0票数 174

我想使用windows命令行(cmd.exe)以编程方式编辑文件内容。在*nix中,有用于此任务的sed。在windows中有没有什么有用的本机等价物?

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2011-05-17 17:35:12

今天powershell救了我。

对于grep,有:

代码语言:javascript
复制
get-content somefile.txt | where { $_ -match "expression"}

代码语言:javascript
复制
select-string somefile.txt -pattern "expression"

对于sed,有以下几点:

代码语言:javascript
复制
get-content somefile.txt | %{$_ -replace "expression","replace"}

有关替换PowerShell函数的详细信息,请参阅this Microsoft article

票数 145
EN

Stack Overflow用户

发布于 2008-09-24 14:05:15

UnxUtils为Win32提供sed,GNUWin32也是如此。

票数 23
EN

Stack Overflow用户

发布于 2011-04-20 18:38:51

如果你不想安装任何东西(我假设你想把脚本添加到其他机器上运行的解决方案/程序/等中),你可以尝试创建一个vbs脚本(比方说,replace.vbs):

代码语言:javascript
复制
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

你可以这样运行它:

代码语言:javascript
复制
cscript replace.vbs "C:\One.txt" "Robert" "Rob"

它类似于"bill weaver“提供的sed版本,但我认为这个版本在特殊(‘>

顺便说一句,这篇文章不是我写的,但我想不起来是从哪里弄来的。

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/127318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档