为了账号安全,请及时绑定邮箱和手机立即绑定

请问是否有用于cmd.exe的sed类实用程序?

请问是否有用于cmd.exe的sed类实用程序?

慕田峪4524236 2019-10-20 16:08:07
是否有用于cmd.exe的sed类实用程序?我希望使用windows命令行以编程方式编辑文件内容(cmd.exe)。在*nix中SED完成这些任务。窗户里有等效的东西吗?
查看完整描述

3 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

今天Powershell救了我。

grep有:

get-content somefile.txt | where { $_ -match "expression"}

而为了sed有:

get-content somefile.txt | %{$_ -replace "expression","replace"}

有关更多细节,请参见Zain Naboulsis博客.



查看完整回答
反对 回复 2019-10-21
?
哔哔one

TA贡献1854条经验 获得超8个赞

如果您不想安装任何东西(我假设您希望将脚本添加到将在其他机器上运行的某个解决方案/程序/etc中),您可以尝试创建一个VBS脚本(例如,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

你就像这样运行它:

cscript replace.vbs "C:\One.txt" "Robert" "Rob"

它类似于“Billweaver”提供的sed版本,但我认为这个版本在特殊(‘></)字符方面更友好。

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



查看完整回答
反对 回复 2019-10-21
  • 3 回答
  • 0 关注
  • 527 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信