Powershell 数组索引计算为null-查找行号并将其用作索引号
我的输入文件如下所示:Powershell 数组索引计算为null-查找行号并将其用作索引号,powershell,Powershell,我的输入文件如下所示: #EXTM3U #EXTI NF:-1 tvg-name="Das Erste HD" tvg-id="ARD.de" group-title="Vollprogramm" tvg-logo="https://raw.githubusercontent.com/jnk22/kodinerds-iptv/master/logos/tv/daserstehd.png",Das Erste HD https://daserstehdde-lh.akamaihd.net/i/da
#EXTM3U
#EXTI NF:-1 tvg-name="Das Erste HD" tvg-id="ARD.de" group-title="Vollprogramm" tvg-logo="https://raw.githubusercontent.com/jnk22/kodinerds-iptv/master/logos/tv/daserstehd.png",Das Erste HD
https://daserstehdde-lh.akamaihd.net/i/daserstehd_de@625196/index_2739_av-p.m3u8
#EXTINF:-1 tvg-name="Das Erste" tvg-id="ARD.de" group-title="Vollprogramm" tvg-logo="https://raw.githubusercontent.com/jnk22/kodinerds-iptv/master/logos/tv/daserste.png",Das Erste
https://daserstelive-lh.akamaihd.net/i/daserste_de@28289/index_3776_av-p.m3u8
..
我想用变量$newRtsp
替换流,并将其写回文件。我目前拥有的是:
# Getting the file
$inFile = Get-Content "iptv.m3u"
# Linenumber where a specific channel name $chanName exists
$line = ($inFile | Select-String "tvg-name=`"$chanName`"").LineNumber #`# dummy comment to fix code highlighting for SO
# Use actual Linenumber as Index, to override the following line
$inFile[$line] = $newRtsp
# Write back to file
$inFile | Set-Content "iptv.m3u"
问题:不知何故,我无法将找到的行号用作索引:
Index operation failed; the array index evaluated to null.
At E:\merge-fritzbox.ps1:17 char:5
+ $inFile[$line] = $newRtsp
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
返回的行号从
1
开始计数。数组索引从<代码> 0 /代码>中计数,因此您需要考虑。
这应该起作用:
$chanName = "Das Erste HD"
$newRtsp = "the new content for the line"
# Getting the file
$inFile = Get-Content "iptv.m3u"
# Linenumber where a specific channel name $chanName perhaps exists?
# SimpleMatch is an optional parameter and specifies that
# the string in the pattern is not interpreted as a regular expression.
$line = ($inFile | Select-String "tvg-name=`"$chanName`"" -SimpleMatch).LineNumber #`# dummy comment to fix code highlighting for SO
# $line will be $null if Select-String did not find the string to look for
if ($line) {
# Use actual Linenumber minus 1 as Index, to override the following line
$inFile[$line - 1] = $newRtsp
# Write back to file
$inFile | Set-Content "iptv.m3u"
}
你怎么知道文件中存在特定的
$chanName
?是的,我错过了。没有决定。出于兴趣,您是只想更新一个$newRtsp
,还是想通过替换已更改的源来保持您的个人(已订购)m3u列表最新?我可以通过FritzBox路由器(DVB-C)及其导出。m3u
格式与我在Kodi上使用的格式不同。所以我只是想用本地可用的流替换普通频道。我尝试使用$infle=$infle.IndexOf($($m3u-match'tvg name=“[regex]::Escape($chNameFB”)”)”)
来找到我找到的正确的行号。您的观点是正确的,但不需要-1
,因为我需要RTSP行号(下一行)而不是包含频道标题的那一行。返回的值已经是我的索引。