Jak*_*urc 112
今天powershell救了我.
因为grep有:
get-content somefile.txt | where { $_ -match "expression"}
Run Code Online (Sandbox Code Playgroud)
而且sed有:
select-string somefile.txt -pattern "expression"
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅Zain Naboulsis博客文章.
b w*_*b w 87
sed (及其同类)包含在几个Unix命令包中.
sed,grep等等.-z选项如果您不想安装任何东西并且您的系统不是Windows Server,那么您可以使用脚本语言(例如VBScript).下面是一个粗略的,袖手旁观的刺.你的命令行看起来像
cscript //NoLogo sed.vbs s/(oldpat)/(newpat)/ < inpfile.txt > outfile.txt
Run Code Online (Sandbox Code Playgroud)
其中oldpat和newpat是Microsoft vbscript正则表达式模式.显然我只是实现了替换命令并假设了一些东西,但你可以充实它,使其变得更聪明,更了解sed命令行.
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
Run Code Online (Sandbox Code Playgroud)
小智 17
如果你不想安装任何东西(我假设你想将脚本添加到将在其他机器上运行的某个解决方案/程序/等),你可以尝试创建一个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
Run Code Online (Sandbox Code Playgroud)
你运行它是这样的:
cscript replace.vbs "C:\One.txt" "Robert" "Rob"
Run Code Online (Sandbox Code Playgroud)
这类似于"bill weaver"提供的sed版本,但我认为这个特殊('> </)字符更友好.
顺便说一句,我没有写这个,但我不记得我从哪里得到它.
小智 7
> (Get-content file.txt) | Foreach-Object {$_ -replace "^SourceRegexp$", "DestinationString"} | Set-Content file.txt
Run Code Online (Sandbox Code Playgroud)
这是行为
sed -i 's/^SourceRegexp$/DestinationString/g' file.txt
Run Code Online (Sandbox Code Playgroud)
我用Cygwin.我遇到很多人没有意识到,如果你把Cygwin二进制文件放在你的PATH上,你可以在Windows Command shell中使用它们.你不必运行Cygwin的Bash.
您也可以查看Microsoft提供的Windows Services for Unix(但仅限于Professional及以上版本的Windows).
edlin 或编辑
另外还有 Windows Services for Unix,它附带了许多用于 Windows 的 Unix 工具。 http://technet.microsoft.com/en-us/interopmigration/bb380242.aspx
更新 12/7/12 在 Windows 2003 R2、Windows 7 和 Server 2008 等中,上述内容被作为附加组件的基于 UNIX 应用程序的子系统 (SUA) 所取代。但您必须下载实用程序: http://www.microsoft.com/en-us/download/details.aspx ?id=2391
有一个名为 Windows 的帮助程序批处理文件repl.bat,它具有 SED 但或安装的大部分功能doesn't require any additional download。它是一个混合批处理文件,用于实现批处理的Jscript功能以及swift轻松处理空行。doesn't suffer from the usual poison characters
repl从 - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat下载
替代链接 - https://www.dostips.com/forum/viewtopic.php?f=3&t=6044
作者是来自 stack Overflow 和 dostips.com 的 @dbenham
另一个名为 的辅助批处理文件findrepl.bat为 Windows 用户提供了许多功能,GREP并且也基于Jscript混合批处理文件,并且同样是混合批处理文件。它具有 repl.bat 的优点
findrepl从 - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat下载
作者是来自 stack Overflow 和 dostips.com 的 @aacini
| 归档时间: |
|
| 查看次数: |
195454 次 |
| 最近记录: |