gpt4 book ai didi

python - 如何在 powershell 中搜索字符串并删除该行?

转载 作者:太空宇宙 更新时间:2023-11-03 17:33:26 25 4
gpt4 key购买 nike

如果我的文本文件中有 5 个单词

<小时/>
red car

yellow flower

purple heart

vault of grass

hard light

我如何搜索这个词,然后删除它并保存/覆盖...在 python 中很容易,但 powershell 对我来说有点复杂。

到目前为止,这就是我所拥有的

#name new file
$fileName = read-host "Enter fileName: "

#create new text file to save processes
$textFile = New-Item C:\Users\socrateslouis\Documents\$folderName\$fileName".txt" -type file

#store in text file
$outfile =Get-Process | Out-File -FilePath C:\Users\socrates\Documents\$folderName\$fileName".txt"
#display processes

$processFile = Get-Content C:\Users\socrates\Documents\$folderName\$fileName".txt"

$processFile

最佳答案

$File = 'TestFile.txt'
$tempFile = 'TestFileTmp.txt'
$stringToMatch = "vault"

Get-Content $File | Where { $_ -notmatch $stringToMatch } | Set-Content $tempFile
Remove-Item -Path $File
Rename-Item -Path $tempFile -NewName $File -Force

关于python - 如何在 powershell 中搜索字符串并删除该行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31551448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com