Replace a changing string in a text file

Hi,

I have script, which fetches a text file (which is a script of a database) into a variable.  Here is a portion of that script:
$DBScriptFile = $Path[0] + $dbname + '\01-DB-' + $dbname + '-' + $Date + '.sql'
$string = Get-Content $DBScriptFile

Inside the file I have strings like:
SIZE = 76288MB
FILEGROWTH = 512MB

I need to replace them with:
SIZE = 1024KB
FILEGROWTH = 1024KB

The problem is, those numbers may be different, e.g.:
SIZE = 10000KB
FILEGROWTH = 2048KB

So I need to replace everything which looks like:
SIZE = XYZB
FILEGROWTH = XYZB
with:
SIZE = 1024KB
FILEGROWTH = 1024KB

Any ideas?

Thanks

Parents
No Data
Reply
  • Thanks guys for your help, however both methods truncate some text in the file.  My original file has many strings like this:

    ( NAME = N'MyDB_201212', FILENAME = N'C:\Path\MyDB_201212.ndf' , SIZE = 76288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 512KB ),

    When I run: 

    $string | ForEach-Object{

        If($_ -match "SIZE\s="){

            "SIZE = 1024KB"

        }ElseIf($_ -match "FILEGROWTH\s="){

            "FILEGROWTH = 1024KB"

        }Else{

            $_

        }

    I get strings like this:

    SIZE = 1024KB

     FILEGROUP [MyDB_201212]

     

    When I run 

    $string -replace '(SIZE|FILEGROWTH)\s=.+','$1 = 1024KB'

    I get strings like this: 

    ( NAME = N'MyDB_201212', FILENAME = N'C:\Path\MyDB_201212.ndf' , SIZE = 1024KB

     

    Any ideas?

     

    Thanks.

     

Children
No Data