Tushar Raichand
Tushar Raichand

Reputation: 61

How to remove specific words from a powershell script?

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

Answers (1)

Shamus Berube
Shamus Berube

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

Related Questions