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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:22:39  来源:igfitidea点击:

Is there any sed like utility for cmd.exe?

windowssedcmd

提问by Jakub ?turc

I want to programmatically edit file content using windows command line (cmd.exe). In *nix there is sedfor this tasks. Is there any useful native equivalent in windows?

我想使用 Windows 命令行(cmd.exe)以编程方式编辑文件内容。在 *nix 中有用于此任务的sed。Windows 中是否有任何有用的本机等价物?

采纳答案by Jakub ?turc

Today powershell saved me.

今天powershell救了我。

For grepthere is:

因为grep有:

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

or

或者

select-string somefile.txt -pattern "expression"

and for sedthere 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 命令包中。

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 sedcommand-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

UnxUtilsprovides sed for Win32, as does GNUWin32.

UnxUtils为 Win32 提供 sed,GNUWin32也是如此

回答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

There is Super Sedan enhanced version of sed. For Windows this is a standalone .exe, intended for running from the command line.

Super Sedsed的增强版。对于 Windows,这是一个独立的 .exe,用于从命令行运行。

回答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。它是一个查找和替换文本实用程序,可用于命令批处理程序。

http://sourceforge.net/projects/fart-it/

http://sourceforge.net/projects/fart-it/

回答by James Boother

You could try powershell. There are get-contentand set-contentcommandlets build in that you could use.

你可以试试powershell。有获得内容设定内容的命令行开关建立,你可以使用。

回答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。