【问题标题】:Powershell: I'm unable to delete a line from a text filePowershell:我无法从文本文件中删除一行
【发布时间】:2020-04-05 02:54:47
【问题描述】:

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

#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"

我是否错误地使用了 -notmatch?这是上下文的全文脚本。

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 第一行的值

$line = $inFile[0]

这是一个 youtube 网址:https://www.youtube.com/watch?v=sB5zlHMsM7k

我还添加了一些语句来在文件之前输出 $line 的值。有人请指点我正确的方向。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我是否错误地使用了-notmatch?

    如果$line 包含一个子字符串以在输入文件的行中搜索 literally(原样,逐字),并且该子字符串恰好包含 正则表达式元字符,例如. 和$。

    要使用-match / -notmatch 进行literal 子字符串匹配,您必须转义 子字符串:

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

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

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

    请注意,PowerShell 没有用于 literal 子字符串匹配的运算符。

    然而,System.String ([string]) 类型有一个 .Contains() 方法用于文字子字符串匹配,但与 PowerShell 的运算符不同,它默认区分大小写(有是不区分大小写匹配的重载):

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

    对于全行匹配:

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

    使用.Contains() 的优势在于它的性能优于-match,尽管后者提供了更大的灵活性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 2016-05-15
      • 2013-05-13
      • 2023-02-08
      相关资源
      最近更新 更多