解决方案
今天,powershell救了我。
对于grep
,有:
get-content somefile.txt | where { $_ -match "expression"}
和sed
有:
get-content somefile.txt | %{$_ -replace "expression","replace"}
有关详细信息,请参阅 Zain Naboulsis blog进入。
其他提示
sed
(及其同类)中包含的几个包Unix命令。
- Cygwin 但是巨大的。
- UnxUtils 是很渺茫。
- GnuWin32 是另一个港口。
- 另一种选择是AT&T研究的 UWIN系统.
- 不能马 从MinGw又是另一种选择。
- 窗子系统,用于Linux 是一个大多数的"当地"的选择,但这不上安装了Windows默认情况下;它已
sed
,grep
等等。开箱,虽然。 - https://github.com/mbuilov/sed-windows 提供了近4.3和4.4版本,支持
-z
选项 不同于列出上口
如果你不想要安装任何东西和你的系统不是一个Windows服务器的一个,那么你 可能 使用一个脚本语言(VBScript例如)。下面是一个严重的箍带刺。你的命令行会看起来像
cscript //NoLogo sed.vbs s/(oldpat)/(newpat)/ < inpfile.txt > outfile.txt
在oldpat和newpat是 Microsoft vbscript regex patterns.显然我只实施的替换指令,并担一些事情,但你可以肉体出来要聪明和了解更多的 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
如果您不想安装任何东西(我假设您要将脚本添加到将在其他计算机上运行的某个解决方案/程序/等),您可以尝试创建一个vbs脚本(假设,替换。 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"
这类似于<!> quot; bill weaver <!>“提供的sed版本,但我认为这个特别方便('<!> gt; <!> lt; / )人物。
是的,我没有写这个,但我不记得我从哪里得到它。Super Sed 是sed的增强版。对于Windows,这是一个独立的.exe,用于从命令行运行。
尝试使用fart.exe。它是一个可以在命令批处理程序中使用的查找和替换文本实用程序。
> (Get-content file.txt) | Foreach-Object {$_ -replace "^SourceRegexp$", "DestinationString"} | Set-Content file.txt
这是
的行为sed -i 's/^SourceRegexp$/DestinationString/g' file.txt
你可以安装Cygwin( http://www.cygwin.com/ )并从那里使用sed
您可以尝试使用PowerShell。有 get-content 和 set-content 命令行开关。
我使用 Cygwin 。我遇到很多人没有意识到如果你把Cygwin二进制文件放在你的PATH上,你可以在Windows Command shell中使用它们。你不必运行Cygwin的Bash。
您还可以查看Microsoft提供的 Windows Services for Unix (但仅限于专业版及以上版本的Windows)。
edlin或edit
此外还有适用于Windows的Windows服务,其中包含许多用于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/下载/ details.aspx?ID = 2391
您可以查看 GNU工具,它们提供(除其他事项外)Windows上的sed。
有一个名为repl.bat
的Windows帮助程序批处理文件,它具有SED的许多功能,但doesn't require any additional download
或安装。它是一个混合批处理文件,使用Jscript
来实现这些功能,因此swift
,doesn't suffer from the usual poison characters
批处理并轻松处理空行。
从 - https://www.dropbox.com/s/下载repl
qidqwztmetbvklt / repl.bat
作者是来自堆栈溢出的@dbenham和dostips.com
另一个名为findrepl.bat
的帮助程序批处理文件为Windows用户提供了GREP
的大部分功能,并且也基于findrepl
,同样也是一个混合批处理文件。它分享了repl.bat
从 - https://www.dropbox.com/s/下载<=> rfdldmcb6vwi9xc / findrepl.bat
作者是来自堆栈溢出的@aacini和dostips.com
据我所知,sed与windows捆绑在一起。但是,sed可以以多种不同的形式用于Windows,包括作为Cygwin的一部分,如果你想要一个完整的POSIX子系统,或者如果你想在命令行上运行sed作为Win32本机可执行文件。
如果它需要是Windows原生的,那么我唯一可以建议的是使用Windows支持的脚本语言,而不使用附加组件,例如VBScript。
Cygwin可以使用,但也可以使用这些实用程序。只需将它们放在您的驱动器上,将目录放入您的路径中,您就拥有许多友好的unix实用程序。更轻量级的恕我直言,Cygwin(虽然它的效果也一样)。
我需要一个适用于Windows cmd.exe提示符的sed工具。 Eric Pement的 sed 端口到单个DOS .exe 对我来说很有用
记录非常好。
这适用于Vista Ultimate,不确定Pro。
sed -f commandfilename.cmd file1 > file2