[英]How do I search a file line by line and then copy a line to a list if it contains a certain string?
[英]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.