PowerShell:我无法从文本文件中删除一行

gijlo24d  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(194)

以下是一种删除我认为可行的行的方法


# earlier in the script

$inFile = Get-Content -Path ".\input.txt"

# ...Later... #

$inFile = Get-Content -path ".\input.txt" | where-object {$_ -notmatch $line}
set-content -path ".\input.txt" -Value $inFile

问题是-NotMatch参数似乎不起作用。Get-Content cmdlet只复制input.txt中的所有内容,包括$line。我还尝试更改代码以完全清除$inFile并创建一个临时持有者,但没有结果。

Clear-Variable -name "inFile"
$holder = Get-Content -path ".\input.txt" | where-object {$_ -notmatch $line}
set-content -path ".\input.txt" -Value $holder
$inFile = Get-Content -path ".\input.txt"

我是不是用错了-Not Match?以下是上下文的全文脚本。

Write-Host "Starting"

[bool] $keepRunning = 1
[bool] $everFound = 0
[bool] $searchComplete = 0
:main while($keepRunning)
{
    $inFile = Get-Content -path ".\input.txt"
    $completed = Get-Content -Path ".\output.txt"
    $line = $inFile[0]
    $holder
    if($inFile.count -eq 1)
    {
        $line = $inFile
    }

    # create condition to check if $line matches any line in completed.txt
    # if it does, skip this line and move on to the next line
    :search while($everFound -eq 0 -and $searchComplete -eq 0)
    {
        #Write-Host "Outer loop"
        foreach($url in $completed)
        {
            #Write-Host $line
            #write-host $url

            if ($line -eq $url)
            {
                Write-Host "`nThis file was already downloaded --Skipping to the next line"
                $inFile = Get-Content -path ".\input.txt" | where-object {$_ -notmatch $line}
                set-content -path ".\input.txt" -Value $inFile
                $inFile = Get-Content -path ".\input.txt"
                $line = $inFile[0]
                $everFound = 1
                break
            }
        }
        if ($everFound -eq 1)
        {
            break
        }
        $searchComplete = 1
        Write-Host "Search Complete`n"
    }
    Write-Host "Before the download--------"

    Write-Host $everFound
    Write-Host $searchComplete

    if ($everFound -eq 0 -and $searchComplete -eq 1)
    {
        #download the files
        $downloadCommand = "youtube-dl.exe --verbose --cookies .\cookies.txt `"$line`""
        get-date
        invoke-Expression $downloadCommand

        #delete the url
        add-content -Path ".\output.txt" -Value $line
        $inFile = Get-Content -path ".\input.txt" | where-object {$_ -notmatch $line} 
        set-content -path ".\input.txt" -Value $inFile
        write-host "`n"

        get-date
        Write-Host "Sleeping for 45mins"
        #start-sleep -s 2700
    }
    $everFound = 0
    $searchComplete = 0
    Write-Host "-------------After the download!!"
    Write-Host $everFound
    Write-Host $searchComplete

    # check if the file is empty. If it is, set the keepRunning flag to false and exit the main while loop
    if($Null -eq $inFile)
    {
        $keepRunning = 0
    }
}

Write-Host "Done"
Read-Host "Press the Enter Key to Exit"

编辑:$inFile在每一行都包含一个YouTube URL列表。$line被赋予$inFile第1行的值

$line = $inFile[0]

以下是YouTube的URL:https://www.youtube.com/watch?v=sB5zlHMsM7k
我还在文件前面添加了一些语句来输出$line的值。谁给我指个方向好吗?

jslywgbw

jslywgbw1#

我是否错误地使用了-notmatch
如果$line在输入文件的行中包含要搜索的字面意思(按原样、逐字)搜索的子字符串,并且该子字符串恰好包含regex元字符,例如.$,则说明您错误地使用了它。
要使用-match/-notmatch进行文字子字符串匹配,您必须对子字符串进行*转义:

$_ -notmatch [regex]::Escape($line)

如果只想完全匹配行,则必须锚定正则表达式:

$_ -notmatch ('^' + [regex]::Escape($line) + '$')

请注意,PowerShell没有用于文字子字符串匹配的运算符。
然而,System.String([string])类型有一个用于字面子字符串匹配的.Contains()方法,但与PowerShell的运算符不同,它在默认情况下区分大小写(存在不区分大小写的匹配重载,但仅在PowerShell(Core)7+中):

-not $_.Contains($line) # case-sensitive, literal substring matching

# PS 7+ only: case-INsensitive, literal substring matching

-not $_.Contains($line, 'CurrentCultureIgnoreCase')

对于整行匹配:

-not ($_.Length -eq $line.Length -and $_.Contains($line))

或者:

-not $_.Equals($line, 'CurrentCultureIgnoreCase')

使用.Contains()的优势是它比-match执行得更好,尽管后者提供了更多的灵活性。

相关问题