【问题标题】:delete everything after keyword删除关键字后的所有内容
【发布时间】:2019-01-24 16:04:41
【问题描述】:

我尝试使用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 时重复。除了具有不同的价值。 我想删除关键字“数字”和关键字本身之后的所有内容(所有内容不仅意味着“= 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

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

【问题讨论】:

  • 您能否展示您提供的示例输入之前的预期结果。
  • 当然,所以一切,意味着每一行,在一个关键字之后应该被删除

标签: powershell ini compareobject


【解决方案1】:

这可能会有所帮助:

# 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

【讨论】:

  • 谢谢,这完美解决了我删除单行的问题。谢谢!
猜你喜欢
  • 2022-06-28
  • 2012-10-19
  • 2011-08-03
  • 2017-08-26
  • 1970-01-01
  • 2023-02-20
  • 2017-06-03
  • 1970-01-01
  • 2017-12-31
相关资源
最近更新 更多