windows cmd.exe 是否有类似 sed 的实用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/127318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any sed like utility for cmd.exe?
提问by Jakub ?turc
采纳答案by Jakub ?turc
Today powershell saved me.
今天powershell救了我。
For grep
there is:
因为grep
有:
get-content somefile.txt | where { $_ -match "expression"}
or
或者
select-string somefile.txt -pattern "expression"
and for sed
there is:
因为sed
有:
get-content somefile.txt | %{$_ -replace "expression","replace"}
For more detail see Zain Naboulsis blog entry.
有关更多详细信息,请参阅Zain Naboulsis 博客条目。
回答by b w
sed
(and its ilk) are contained within several packages of Unix commands.
sed
(及其同类)包含在几个 Unix 命令包中。
- Cygwinworks but is gigantic.
- UnxUtilsis much slimmer.
- GnuWin32is another port that works.
- Another alternative is AT&T Research's UWIN system.
- MSYSfrom MinGw is yet another option.
- Windows Subsystem for Linuxis a most "native" option, but it's not installed on Windows by default; it has
sed
,grep
etc. out of the box, though. - https://github.com/mbuilov/sed-windowsoffers recent 4.3 and 4.4 versions, which support
-z
optionunlike listed upper ports
- Cygwin可以工作,但非常庞大。
- UnxUtils要精简得多。
- GnuWin32是另一个有效的端口。
- 另一种选择是 AT&T Research 的UWIN 系统。
- MinGw 的MSYS是另一种选择。
- 适用于 Linux 的 Windows 子系统是最“原生”的选项,但默认情况下它不会安装在 Windows 上;不过
sed
,它grep
开箱即用。 - https://github.com/mbuilov/sed-windows提供最新的 4.3 和 4.4 版本,支持
-z
选项不同于上面列出的端口
If you don't want to install anything and your system ain't a Windows Server one, then you coulduse a scripting language (VBScript e.g.) for that. Below is a gross, off-the-cuff stab at it. Your command line would look like
如果您不想安装任何东西并且您的系统不是 Windows Server 系统,那么您可以为此使用脚本语言(例如 VBScript)。下面是粗暴的、即兴的刺伤。你的命令行看起来像
cscript //NoLogo sed.vbs s/(oldpat)/(newpat)/ < inpfile.txt > outfile.txt
where oldpat and newpat are Microsoft vbscript regex patterns. Obviously I've only implemented the substitute command and assumed some things, but you could flesh it out to be smarter and understand more of the sed
command-line.
其中 oldpat 和 newpat 是Microsoft vbscript regex 模式。显然,我只实现了替换命令并假设了一些东西,但是您可以充实它以使其更聪明并了解更多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
回答by Adam Hughes
回答by Rober
If you don't want to install anything (I assume you want to add the script into some solution/program/etc that will be run in other machines), you could try creating a vbs script (lets say, replace.vbs):
如果你不想安装任何东西(我假设你想将脚本添加到一些将在其他机器上运行的解决方案/程序/等),你可以尝试创建一个 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
And you run it like this:
你像这样运行它:
cscript replace.vbs "C:\One.txt" "Robert" "Rob"
Which is similar to the sed version provided by "bill weaver", but I think this one is more friendly in terms of special (' > < / ) characters.
这类似于“bill weaver”提供的sed版本,但我认为这个版本在特殊('> </)字符方面更友好。
Btw, I didn't write this, but I can't recall where I got it from.
顺便说一句,我没有写这个,但我不记得我从哪里得到的。
回答by Rob Kam
回答by krogon
> (Get-content file.txt) | Foreach-Object {$_ -replace "^SourceRegexp$", "DestinationString"} | Set-Content file.txt
This is behaviour of
这是行为
sed -i 's/^SourceRegexp$/DestinationString/g' file.txt
回答by Colin Nicholls
Try fart.exe. It's a Find-and-replace-text utility that can be used in command batch programs.
试试放屁.exe。它是一个查找和替换文本实用程序,可用于命令批处理程序。
回答by James Boother
You could try powershell. There are get-contentand set-contentcommandlets build in that you could use.
回答by Brandon DuRette
I use Cygwin. I run into a lot of people that do not realize that if you put the Cygwin binaries on your PATH, you can use them from within the Windows Command shell. You do not have to run Cygwin's Bash.
我使用Cygwin。我遇到了很多人,他们没有意识到如果将 Cygwin 二进制文件放在 PATH 上,则可以在 Windows 命令外壳中使用它们。您不必运行 Cygwin 的 Bash。
You might also look into Windows Services for Unixavailable from Microsoft (but only on the Professional and above versions of Windows).
您还可以查看Microsoft 提供的适用于 Unix 的 Windows 服务(但仅限于专业版和更高版本的 Windows)。
回答by robintw
You could install Cygwin (http://www.cygwin.com/) and use sed from there.
您可以安装 Cygwin ( http://www.cygwin.com/) 并从那里使用 sed。