【问题标题】:How to do a "sed -" like operation on Windows?如何在 Windows 上进行类似“sed-”的操作?
【发布时间】:2012-12-16 00:25:28
【问题描述】:

我有一个 55 GB 的大文件,其中每一行都有一个句子。 我想检查是否有任何带有点“。”的行。最后,如果有,我想在该行的点之前插入一个空格。

例如:我喜欢那辆车。 替换为:我喜欢那辆车。

如果有点,则每行尾随点前有一个空格。

我没有任何 cygwin 或 unix,我使用的是 Windows 操作系统。我可以在这 55GB 上做一个常见的 sed 吗?文件?

我尝试了 GetGNUWin32,但无法确定那里的实际命令。

【问题讨论】:

    标签: windows unix sed


    【解决方案1】:

    安装 Perl。 Strawberry Perl 可能是最好的 Windows 发行版。 http://strawberryperl.com/

    要在 Perl 中执行您所说的操作,应该是这样的:

    perl -p -i -e's/\.$/ ./' filename
    

    【讨论】:

    • 我试过了,但它在 Windows 7 上没有做任何事情,经过一些实验后我发现我必须使用双引号 " 而不是单引号 '。
    【解决方案2】:

    您可以从那里安装Cygwin 并使用sed。在这里我找到了Sed for Windows


    编辑
    很好的回答你的问题: Is there any sed like utility for cmd.exe

    (当我在 google 上搜索时,我总是在 stackoverfloew 前加上前缀。我在 google 上为你做了同样的事情:sed on window stackoverflow,但那是另一回事)

    【讨论】:

    • 对于 Google,您也可以添加 inurl 或站点运算符,@GrijeshChauhan
    【解决方案3】:

    对于您的用例:

    来自 PowerShell.exe(Windows 自带)

    (Get-Content file.txt) -Replace '\.$', ' .' | Set-Content file.txt
    

    我搜索了好几个小时,在为我的用例找到解决方案时遇到了很多麻烦,所以我希望添加这个答案可以帮助遇到同样情况的其他人。

    对于那些像我一样弄清楚 git filter clean/smudge 的人来说,这是我最终管理它的方法:

    在文件中:.gitconfig(全局)

    [filter "replacePassword"]
        required = true
        clean = "PowerShell -Command \"(Get-Content " %f ") -Replace 'this is a password', 'this is NOT a password'\""
        smudge = "PowerShell -Command \"(Get-Content " %f ") -Replace 'this is NOT a password', 'this is a password'\""
    

    请注意,这个 sn-p 不会更改原始文件(这是为我的用例准备的)。

    额外的搜索词来帮助那些寻找:插值,插值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多