[英]Delete from file block of lines
我有一个文本文件,其中的信息ID以行的形式组织。 我需要删除包含一些信息的块。
例如我有以下
dn: CN=Publishers,OU=ABC - Groups
changetype: add
cn: Cert
description: Members of this group are permitted to ...
groupType: -2
objectClass: top
objectClass: group
dn: CN=Domain Guests,OU=ABC - Groups,
changetype: add
cn: Domain Guests
description: All domain guests
groupType: -21
objectClass: top
objectClass: group
dn: CN=Domain Computers,OU=ABC- Groups
changetype: add
cn: Domain Computers
description: All workstations
groupType: -2
objectClass:top
objectClass: group
dn: CN=AS Servers,OU=ABC- Groups
changetype: add
cn: AS Servers
description: Servers in this group...
groupType: -214
objectClass: top
objectClass: group
dn: CN=Domain Controllers,OU=ABC - Groups
changetype: add
cn: Domain Controllers
description: All domain controllers in the domain
groupType: -21
objectClass: top
objectClass: group
dn: CN=Domain Users,OU=ABC - Groups
changetype: add
cn: Domain Users
description: All domain users
groupType: -21
objectClass: top
objectClass: group
并且我需要删除例如名称包含在另一个文件中的块,例如某些块的第一行在其他文件中(例如, CN=Domain Computers,OU=ABC- Groups and CN=AS Users,OU=ABC- Groups (those bellow)
我的代码:
$listsharedCN=Get-content "shared.txt
$exported_groups= Get-Content "groups.txt
$listsharedCN | % {
$var=($_ -split '`n')[0]
if(($exported_groups | % { ($_ -split '`n')[0] }) -match[regex]::Escape($var))
{
#I found the first line of the block but need to delete this block "
}
谁能帮我?
dn:\s+CN=(?:Domain\s+Computers|AS\s+Users),OU=ABC\s*-\s+Groups.*?\n
您可以尝试此操作。用empty string
替换。请参见演示。
目前尚不清楚您要做什么,所以我会给您一些一般性的指导。 您可以将此文件转换为CSV格式的文件,然后使用ConvertFrom-Csv读取。 然后,您可以使用一系列适当的对象。
示例(您可以将所有这些都放在一行上):
Get-Content .\ExportGroups.ldf | % { $_ -replace ' (\w+:)', ';' -replace 'dn: ', ''} |
ConvertFrom-Csv -Delimiter ';' -Header DN, ChangeType, CN, Description, GroupType, ObjectClass, ObjectClass2
我会解释一下,因为它有点神秘。
虽然我不确定您对这些数据的用途是什么,但是如果它是对象,则操作起来会容易得多。……请这样做! 从评论中:在我发现事实之后,我对数据的处理方式让我感到非常麻烦。 虽然这应该可行,但我建议如果您有任何问题要在输入数据中放入一些代码(如果需要)并按要求输出。
$rawFile = Get-Content -Raw -Path E:\temp\data.txt
$excludesFile = Get-Content -Path E:\temp\shared.txt
$ldifdes = $rawFile -split '\s+(?=dn:)' | ForEach-Object{
# Empty hashtable that we will use to build a single custom object
$props = @{}
$entityObject = $_ -split "`r`n"
# Parse out the object class. Custom object cannot have more than one key
# so we convert to Array instead.
$entityObject | Where-Object{$_ -notmatch 'objectclass'} | ForEach-Object{
# Populate the object that are not objectclass into $props
$parameter = $_ -split ":"
$props.($parameter[0]) = ($parameter[1]).Trim()
}
# Address the objectclass now.
$props.ObjectClass = @()
$entityObject | Where-Object{$_ -match 'objectclass'} | ForEach-Object{
$props.ObjectClass += (($_ -split ":")[1]).Trim()
}
New-Object -TypeName pscustomobject -Property $props
}
# Filter out the entries we do not need.
$regex = "($($excludesFile -join "|"))"
$ldifdes | Where-Object{$_.DN -notmatch $regex} | ForEach-Object{
Write-Output "dn: $($_.DN)"
Write-Output "changetype: $($_.changetype)"
Write-Output "cn: $($_.cn)"
Write-Output "description: $($_.description)"
Write-Output "groupType: $($_.groupType)"
$_.ObjectClass | ForEach-Object{
Write-Output "ObjectClass: $_"
}
# Add a blank
Write-Output ""
} | Out-File -Encoding ascii -FilePath "E:\temp\output.txt"
如果您至少具有powershell 3.0,则此代码将起作用。 如果需要,降级没什么大不了的。 无需赘述:
Where-Object
解析它们 Write-Ouput
来显示ldifde的数据 Out-File
复杂程度降低
$rawFile = Get-Content -Raw -Path E:\temp\data.txt
$excludesFile = Get-Content -Path E:\temp\shared.txt
$regex = "($($excludesFile -join "|"))"
$results = $rawFile | select-string -pattern '(?smi)(dn:).*?(?=dn:|\Z)' -AllMatches | Foreach {$_.Matches} | ForEach-Object {$_.Value.Trim()}
$results | Where-Object{ $_ -notmatch $regex } | ForEach-Object{$_ + [Environment]::NewLine} |
Out-File -Encoding ascii -FilePath "E:\temp\output.txt"
将字符串解析为块组。 像以前一样构建$regex
字符串,并将其用作Where-Object
的排除Where-Object
。 然后只需使用Out-File
输出到文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.