Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag 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
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
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

Avatar of E=mc2

ASKER

Thanks very much.