Reputation: 11
First of all, sorry for my bad English...I'm a native Spanish speaker...
Well, my question is that I have a .m3u file with a bunch of iptv channels url that look like this:
#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
There is a header line with a name (#EXTINF CHANNELNAME) before the URL with the channel (HTTP://URL/NAME/PASSWORD/NUMBER/)
Is there any way to erase the (need to erase)
lines with PowerShell? I need that any lines without a preceding #EXTINF
line to be deleted. I need to do this because all the kodi iptv adoon won't recognize the file if it has lines without a preceding #EXTINF
line.
Upvotes: 1
Views: 102
Reputation: 437090
You can use Select-String
with the -Context
parameter:
#EXTINF:
, replace -Context 0, 1
with -Context 0, 2
.# 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
Upvotes: 0
Reputation: 61028
You could use switch
for this to delete the unwanted lines at high speed:
$m3uFileIn = 'D:\Test\FileToProcess.m3u' # the full path and filename of the original .m3u file
$m3uFileOut = 'D:\Test\FileAsItShouldBe.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
Output:
#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
P.S. This will finalize the file with an empty line appended to it. If you don't want that, add the -NoNewline
switch to the Set-Content
cmdlet
Upvotes: 2
Reputation: 5114
# 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"
New data
#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
Upvotes: 0