Avatar of E=mc2
E=mc2
Flag for Canada asked on

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.

OR

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:  
C:\Users\user\Desktop\folder\file.txt
And the output should be in the same folder but renamed to:
C:\Users\user\Desktop\folder\fileR.txt
PowershellWindows BatchScripting Languages

Avatar of undefined
Last Comment
E=mc2

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
oBdA

PowerShell:
$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

E=mc2

ASKER
Thanks very much.
Your help has saved me hundreds of hours of internet surfing.
fblack61