Forum Discussion
sunny sunny
Oct 01, 2017Copper Contributor
Search and Replace in power shell
Hello, I am new to power shell and i am trying to achive below. I have file test.txt with data as shown below. i want the search for variable2 and update the value as below. select * from table...
Jiri Reznicek
Oct 09, 2017Copper Contributor
How about this?
it will
- make temporary copy of the file (test.tmp)
- read the file line per line
- if the line starts with "variable2" (can contain whitespaces around), line content is updated with new content
- save the result lines back into test.txt
- removes temporary copy of the file
Copy-Item .\test.txt .\test.tmp; Get-Content test.tmp|ForEach-Object {if($_ -match '^ *variable2 *=') {
"variable2=select * from table1 order by column"} else {$_}} | Out-File .\test.txt; Remove-Item .\test.tmp