Reputation: 61
I have a file which contains the below:
Enable succeeded:
[stdout]
root
daemon
bin
sys
sync
games
man
lp
gnats
nobody
systemd-network
systemd-resolve
syslog
messagebus
_apt
lxd
uuidd
dnsmasq
landscape
sshd
pollinate
traichand
[stderr]
I want to remove Enable succeeded: ,[stdout] and [stderr] from this using powershell. Please help
Upvotes: 0
Views: 261
Reputation: 486
This will read the file and replace any of the matching strings with a blank line and then write it back to the file.
$file = Get-Content C:\path\to\file.txt
$file | Foreach-Object {
if ( ($_ -match "\[stderr\]") -or ($_ -match "\[stdout\]") -or ($_ -match "Enable succeeded:")) {
$file[$file.IndexOf("$_")] = ''
}
}
$file | Set-Content C:\path\to\file.txt
Upvotes: 1