How can I search and replace certain word in a .p file ?

Prajan

New Member
Hi All
How can I search and replace a word in a .p file ? I have a .p file and I need to write a program that searches a word given as input by me and replaces that word by the new word given by me.
I tried it to do it using the stream. I got the whole file in a stream too and extracted a single line and find that paticular word too, but that didnt solve my problem since i cant replace that word in that stream and cant save in that .p file.
So i need some ideas for how can i do so ?
Hope to get some ideas .
Regards
Prajan Shrestha
 

Casper

ProgressTalk.com Moderator
Staff member
sed would be a better suggestion if you want to replace the word....
The problem is that you really have to have a specific word and really have to be carefull what you replace. If you replace the word 'her' in a .p file then 'where' would be replaced too, for instance...
But here is some quick-and-dirty-without-any-checks-sample-code to get you started:

Code:
DEFINE STREAM sIn.
DEFINE STREAM sOut.
 
DEFINE VARIABLE cDir AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cFile AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cData AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cOldWord AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cNewWord AS CHARACTER   NO-UNDO.
 
ASSIGN cDir = 'c:\temp'
       cFile = 'test.p'
       cOldword = ' OR'
       cNewWord = ' AND'.
 
INPUT STREAM sIn FROM VALUE(cDir + '\' + cFile).
OUTPUT STREAM sOut TO VALUE(cDir + '\new' + cFile ).
REPEAT:
    IMPORT STREAM sIn UNFORMATTED cData.
    IF cData MATCHES '*' + cOldWord + '*' 
    THEN ASSIGN cData = REPLACE(cData,cOldWord,cNewWord).
    PUT STREAM sOut UNFORMATTED cData SKIP.
END.
 
INPUT STREAM sIn CLOSE.
OUPUT STREAM sOut CLOSE.

HTH,

Casper.
 

tamhas

ProgressTalk.com Sponsor
The solution to the problem of inappropriate replacements is to base your substitution on syntactically analyzed structures. Look up Proparse, ProLint, and ProRefactor at oehive.org.
 
Top