发布于 2011-05-17 17:35:12
今天powershell救了我。
对于grep
,有:
get-content somefile.txt | where { $_ -match "expression"}
或
select-string somefile.txt -pattern "expression"
对于sed
,有以下几点:
get-content somefile.txt | %{$_ -replace "expression","replace"}
有关替换PowerShell函数的详细信息,请参阅this Microsoft article。
发布于 2011-04-20 18:38:51
如果你不想安装任何东西(我假设你想把脚本添加到其他机器上运行的解决方案/程序/等中),你可以尝试创建一个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"
它类似于"bill weaver“提供的sed版本,但我认为这个版本在特殊(‘>
顺便说一句,这篇文章不是我写的,但我想不起来是从哪里弄来的。
https://stackoverflow.com/questions/127318
复制相似问题