簡體   English   中英

如何在Powershell中搜索字符串並刪除該行?

[英]How do I search a string in powershell and delete the line?

如果我在一個文本文件中有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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM