Script needed to replace or add carriage return when seeing spefic text

I would like a simple script in .bat, .vbs, or powershell, which will look into a .txt fle and do one of the following, whichever is easier to do:

1.  Whenever it sees specific text such as AD, or DE, or BOG, or STOW..  that it will insert a tilde character (~) just before the text and then save the file with a new name.


2.  Whenever it sees the specific text such as AD, DE, BOG, or STOW, that it would start a new line which starts with those characters, and only start another line when it finds the next set of specific text.

The file resides at:  
And the output should be in the same folder but renamed to:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Here's a small VBS script approach.  Adjust the array to include strings to replace with a leading tilde.

It is in case sensitive replacement mode, if you want case insensitive change the last argument of the Replace() function from 0 to 1.

Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

strInFile = "C:\Users\user\Desktop\folder\file.txt"
strOutFile = "C:\Users\user\Desktop\folder\fileR.txt"

arrSearch = Array("AD", "DE", "BOG", "STOW")

Set objFSO = CreateObject("Scripting.FileSystemObject")

With objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
    strData = .ReadAll
End With

For Each strSearch in arrSearch
    strData = Replace(strData, strSearch, "~" & strSearch, 1, -1, 0)

With objFSO.OpenTextFile(strOutFile, ForWriting, True)
    .Write strData
End With

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
$inFile =  'C:\Users\user\Desktop\folder\file.txt'
$outFile = $inFile -replace '(?:\.)([^.]+)\Z', 'R.$1'
Get-Content -Path $inFile  ForEach-Object {$_ -creplace '(AD|DE|BOG|STOW)', '~$1'} | Set-Content -Path $outFile

Open in new window

100questionsAuthor Commented:
Thanks very much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.