删除关键字后的所有内容

delete everything after keyword

我尝试使用 Compare-Object 合并到文件,我得到了这样的文件:

Number=5
Example=4
Track=1000
Date=07/08/2018 19:51:16
MatCaissierePDAAssoc=
NomImpPDAAssoc=
TpeForceLectPan=0
Number=1
Example=1
Track=0
Date=01/01/1999

您可以看到它重复 Number=1。除了具有不同的值。 我想删除关键字 "Number" 和关键字本身之后的所有内容(所有内容不仅意味着“= 1”)。

这是我目前所做的:

$files = Get-ChildItem "D:\all"
foreach ($file in $files) {
    $name = dir $file.FullName | select -ExpandProperty Name

    Compare-Object -ReferenceObject (Get-Content D:\original\test.ini) -DifferenceObject (Get-Content $file.FullName) -PassThru |
        Out-File ('D:\output\' + $name)
}

我想删除所有包含 "Track" 和 "Date" 的行。

我的结果应该是这样的:

Number=5
Example=4
MatCaissierePDAAssoc=
NomImpPDAAssoc=
TpeForceLectPan=0

事实上,我需要一些东西来删除我文件中的双键。

这可能有帮助:

# read existing file
$fileContent = Get-Content C:\tmp\so01.txt

# iterate over lines
foreach($line in $fileContent) {
  # filter lines beginning with 'Track' or 'Number'
  if((-not $line.StartsWith('Track')) -and (-not $line.StartsWith('Number'))) {
    # output lines to new file
    $line | Add-Content C:\tmp\so02.txt
  }
}

C:\tmp\so02.txt的内容:

Example=4
Date=07/08/2018 19:51:16
MatCaissierePDAAssoc=
NomImpPDAAssoc=
TpeForceLectPan=0
Example=1
Date=01/01/1999