如果上一行不匹配,则擦除一行?



首先,很抱歉我的英语不好。。。我以西班牙语为母语。。。

好吧,我的问题是,我有一个.m3u文件,里面有一堆iptv频道的url,看起来像这样:

#EXTINF:-1,Discovery ID FHD (E)
http://url/name/password/46562
http://url/name/password/465621 (need to erase)
#EXTINF:-1,Discovery H&H FHD  (E)
http://url/name/password/46563
http://url/name/password/46564 (need to erase)
http://url/name/password/46565 (need to erase)
http://url/name/password/465651 (need to erase)
http://url/name/password/465652 (need to erase)
#EXTINF:-1,Discovery Theater FHD (E)
http://url/name/password/46566 (need to erase)
#EXTINF:-1,Discovery Theater FHD (E)(2)
http://url/name/password/46567

在带有通道(HTTP://URL/name/PASSWORD/NUMBER/(的URL之前,有一个标题行的名称(#EXTINF CHANNELNAME(

有什么方法可以用PowerShell擦除(need to erase)行吗?我需要删除之前没有#EXTINF行的任何行。我需要这样做,因为如果文件的行没有前面的#EXTINF行,那么所有的kodi iptv adoon都不会识别该文件。

您可以使用switch来高速删除不需要的行:

$m3uFileIn   = 'D:TestFileToProcess.m3u'     # the full path and filename of the original .m3u file
$m3uFileOut  = 'D:TestFileAsItShouldBe.m3u'  # this can be the same as $m3uFileIn if you want to overwrite it
$foundExtInf = $false
# parse the file and return only lines starting with '#EXTINF' 
# and the first subsequent non-blank line, 
# and collect these lines in variable $linesToKeep
$linesToKeep = switch -Regex -File $m3uFileIn {
'^#EXTINF:'  { $_ ; $foundExtInf = $true; continue }
'S'         { if ($foundExtInf) { $_ ; $foundExtInf = $false} }
}
# save as (new) file. parameter -PassThru also outputs to console
$linesToKeep -join "`r`n`r`n" | Set-Content -Path $m3uFileOut -Force -PassThru

输出:

#EXTINF:-1,Discovery ID FHD (E)
http://url/name/password/46562
#EXTINF:-1,Discovery H&H FHD  (E)
http://url/name/password/46563
#EXTINF:-1,Discovery Theater FHD (E)
http://url/name/password/46566 (need to erase)
#EXTINF:-1,Discovery Theater FHD (E)(2)
http://url/name/password/46567

附言:这将在文件后附加一行空行来完成文件。如果您不希望这样做,请将-NoNewline开关添加到Set-Contentcmdlet 中

# Change iptv.txt file to your file
$data = Get-Content ".iptv.txt"
$pattern = '^#EXTINF'
$enum = $data.GetEnumerator()
$keep = [System.Collections.ArrayList]::new()
$enum.MoveNext() | Out-Null
while ($enum.Current) {
if ($enum.Current -match $pattern) {
$keep.Add($enum.Current) | Out-Null
$count = 0
$enum.MoveNext() | Out-Null
do {
if (![string]::IsNullOrWhiteSpace($enum.Current) -and $enum.Current -notmatch $pattern ) {
if ($count -lt 1) { $keep.Add($enum.Current) | Out-Null; $count++ }
}
} until($enum.Current -match $pattern -or -not $enum.MoveNext())
}
else {
$keep.Add($enum.Current) | Out-Null
$enum.MoveNext() | Out-Null
}
}
$keep | ForEach-Object {$_ + "`n"} | Out-File ".revised_data.txt"

新数据

#EXTINF:-1,Discovery ID FHD (E)
http://url/name/password/46562
#EXTINF:-1,Discovery H&H FHD  (E)
http://url/name/password/46563
#EXTINF:-1,Discovery Theater FHD (E)
http://url/name/password/46566 (need to erase)
#EXTINF:-1,Discovery Theater FHD (E)(2)
http://url/name/password/46567

您可以将Select-String-Context参数一起使用:

  • 注意:我假设样本行之间的空行只是发布工件;如果以#EXTINF:开始的每一行之后确实存在空行,则用-Context 0, 2替换-Context 0, 1
# Extract the lines of interest from file 'in.m3u'
# and save them to file 'out.m3u'
Select-String '^#EXTINF:' in.m3u -Context 0, 1 | 
ForEach-Object{ $_.Line; $_.Context.PostContext } |
Set-Content out.m3u

最新更新