I’m trying to work out how to do a search and replace to this:
xxxxxxxxxx
[CASE_1] <START_SETUP>
[CASE_2] <START_SETUP>
To
xxxxxxxxxx
[CASE_1] <START_SETUP>
set_attribute attribute CASE_1
[CASE_NUMBER_TWO] <START_SETUP>
set_attribute attribute CASE_NUMBER_TWO
So to add in a new line the text “set_attribute…” plus the contents of the preceding square brackets each time eg. in this case that “] <START_SETUP>” is found.
Assuming your original file is named “foo.txt”, you can regex match for each row and append your additional row as below.
tio
xxxxxxxxxx
Get-Content foo.txt | ForEach-Object {
$_ -match "\[(.*)\]" | Out-Null
$_
"set_attribute attribute $($matches[1])"
}
xxxxxxxxxx
[CASE_1] <START_SETUP>
set_attribute attribute CASE_1
[CASE_2] <START_SETUP>
set_attribute attribute CASE_2
See about_Regular_Expressions for further information on the $matches
automatic variable and regex matching in powershell generally.