Anonymous 2321
I'm trying to work out how to do a search and replace to this:
```
[CASE_1] <START_SETUP>
[CASE_2] <START_SETUP>
```
To
```
[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.
Top Answer
PeterVandivier
Assuming your original file is named "foo.txt", you can regex match for each row and append your additional row as below.
::: tio XYzBCoJAFEX3fsVjcKHBCLYOMcTaZaStVESHFxaTE@OTgurbJ5Ek6G7u5XC4N3VH3bcoJRdKowmZlUfrNK78ElZptj5kVRpnx33wxct/zEII4KSURw8yWyQeqY6wo5nBCzZKx7VoedJcUBA8LRhjV8CvNYkWWJE73sItSja6yUB8N0g5O1OzHqmqifS5GQjht2zHnj6wz/3SZdYbjPkA
§§§ powershell powershell
Get-Content foo.txt | ForEach-Object {
$_ -match "\[(.*)\]" | Out-Null
$_
"set_attribute attribute $($matches[1])"
}
§§§
```
[CASE_1] <START_SETUP>
set_attribute attribute CASE_1
[CASE_2] <START_SETUP>
set_attribute attribute CASE_2
```
:::
See [about_Regular_Expressions](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions#groups-captures-and-substitutions) for further information on the `$matches` automatic variable and regex matching in powershell generally.