是否有任何sed类似于cmd.exe的实用程序 [英] Is there any sed like utility for cmd.exe

查看:771
本文介绍了是否有任何sed类似于cmd.exe的实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Windows命令行( cmd.exe )以编程方式编辑文件内容。在* nix中,有 sed 用于执行此任务。






  • Cygwin 的工作原理

  • UnxUtils 更加纤薄。

  • GnuWin32 是另一个工作的港口。

  • 另一个选择是AT& T Research的 UWIN系统

  • MSGY 是MinGw的另一个选择。

  • Windows Services for UNIX 是最原生的选项,但它已安装默认情况下在Windows Server计算机上;它有 sed grep 等开箱即可。



    • 如果你不想安装任何东西,你的系统不是Windows Server系统,那么你可以使用脚本语言(VBScript eg ) 为了那个原因。下面是一个粗暴,off-the-cuff刺。你的命令行看起来像

        cscript // NoLogo sed.vbs s /(oldpat)/(newpat)/& inpfile.txt> outfile.txt 

      其中oldpat和newpat Microsoft vbscript regex模式。显然,我只实现了replace命令,并假定了一些东西,但你可以更聪明,更容易理解 sed 命令行。

        Dim pat,patparts,rxp,inp 
      pat = WScript.Arguments(0)
      patparts = Split /)
      设置rxp = new RegExp
      rxp.Global = True
      rxp.Multiline = False
      rxp.Pattern = patparts(1)
      WScript.StdIn.AtEndOfStream
      inp = WScript.StdIn.ReadLine()
      WScript.Echo rxp.Replace(inp,patparts(2))
      Loop


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

      Edit: I am looking for native command line solution.

      解决方案

      sed (and its ilk) are contained within several packages of Unix commands.

      • Cygwin works but is gigantic.
      • UnxUtils is much slimmer.
      • GnuWin32 is another port that works.
      • Another alternative is AT&T Research's UWIN system.
      • MSYS from MinGw is yet another option.
      • Windows Services for UNIX is a most "native" option, but it's installed by default on on Windows Server machines; it has sed, grep etc. out of the box, though.

      If you don't want to install anything and your system ain't a Windows Server one, then you could use a scripting language (VBScript e.g.) for that. Below is a gross, off-the-cuff stab at it. Your command line would look like

      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.

      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
      

      这篇关于是否有任何sed类似于cmd.exe的实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆