合并不连续区域的文字

时间:2019-06-21 22:53:44

标签: regex powershell

我需要删除图片1中红色矩形中的内容,然后生成一个新的文本文件。

还将AIDA64 Engineer替换为AIDA64 Ultimate

由于保留字段被划分为不同的区域,例如:主板:,似乎有些困难

在以下帖子中,一些相关问题已解决。 Get strings for some specific region

enter image description here

--------[ AIDA64 Engineer ]------------------------------------------------------------

    version                                              AIDA64 Engineer v6.00.5100/cn

--------[ System verview ]-------------------------------------------------------------

    Motherboard:
      Processor name                                    Mobile DualCore Intel

    DMI:
      DMI BIOS Vendor                                   Phoenix Technologies

--------[ DMI ]------------------------------------------------------------------------

  [ BIOS ]

    BIOS Attributes:
      Vendor                                            Phoenix Technologies Ltd.

  [ Motherboard ]

    Motherboard:
      manufacturer                                      Intel Corp.

    Motherboard manufacturer:
      company name                                      Intel Corporation

--------[ Overclocking ]---------------------------------------------------------------

    Motherboard:
      Motherboard ID                                     <DMI>

    BIOS Attributes:
      System BIOS date                                   12/24/2012

--------[ PCI/PnP Network ]------------------------------------------------------------

    Atheros AR5009 802.11a/g/n Wireless                   PCI
    Broadcom NetLink BCM57785 PCI-E                       PCI

1 个答案:

答案 0 :(得分:3)

由于要删除的字符串似乎没有共同之处,因此长时间进行正则表达式替换可能会有所帮助:

$re = '\s+(DMI:\s+DMI BIOS Vendor|Motherboard manufacturer:\s+company name|BIOS Attributes:\s+System BIOS date)[^-]+'
$nlnl = ([Environment]::NewLine * 2)  # replace with two newlines
(Get-Content -Path 'PATH TO THE FILE' -Raw) -replace $re, $nlnl -replace 'AIDA64 Engineer', 'AIDA64 Ultimate'

结果:

--------[ AIDA64 Ultimate ]------------------------------------------------------------

    version                                              AIDA64 Ultimate v6.00.5100/cn

--------[ System verview ]-------------------------------------------------------------

    Motherboard:
      Processor name                                    Mobile DualCore Intel

--------[ DMI ]------------------------------------------------------------------------

  [ BIOS ]

    BIOS Attributes:
      Vendor                                            Phoenix Technologies Ltd.

  [ Motherboard ]

    Motherboard:
      manufacturer                                      Intel Corp.

--------[ Overclocking ]---------------------------------------------------------------

    Motherboard:
      Motherboard ID                                     <DMI>

--------[ PCI/PnP Network ]------------------------------------------------------------

    Atheros AR5009 802.11a/g/n Wireless                   PCI
    Broadcom NetLink BCM57785 PCI-E                       PCI


编辑

如果您只想替换标题中的AIDA64 Engineer,而不是以后的文本,请更改此部分:

-replace 'AIDA64 Engineer', 'AIDA64 Ultimate'

进入

-replace '\[ AIDA64 Engineer \]', '[ AIDA64 Ultimate ]'