Search and Replace in REXX? 
Author Message
 Search and Replace in REXX?

Hello folks;

    Is there a quick and dirty way to search a file for a string and
replace it with another string?

    Post or E-Mail, please: I don't really know REXX, and I'm pulling my
hair out.

      TIA:
      Mark G. Pomerleau



Fri, 22 May 1998 03:00:00 GMT  
 Search and Replace in REXX?

Mark:

Quote:
>    Is there a quick and dirty way to search a file for a string and
>replace it with another string?

Try:

   ftp://hobbes.nmsu.edu/os2/dev32/rexx/rxmulch1.zip

brief description:

   rxmulch1.zip Jan 31  1995    42039 Replaces/counts strings in files (REXX)

It allows for replacing anything, in case you have several changes to apply,
you'd write them to a control file which rxmulch uses (with a switch you can
reverse the changes too).

The OO-REXX version is 50xtimes faster due to the usage of the newly introduced
function CHANGESTR() (otherwise no changes were applied) !  If that helped
someone, I would upload the newer version to hobbes.

---rony



Sat, 23 May 1998 03:00:00 GMT  
 Search and Replace in REXX?


:>Hello folks;
:>
:>    Is there a quick and dirty way to search a file for a string and
:>replace it with another string?
:>
:>    Post or E-Mail, please: I don't really know REXX, and I'm pulling my
:>hair out.
:>
:>
:>      TIA:
:>      Mark G. Pomerleau

:>

How about

      TEMP = POS(searchfor,string)
      If  TEMP > 0  Then
          string = LEFT(string,TEMP-1) || ,
                   newstring || ,
                   SUBSTR(string,TEMP+LENGTH(searchfor)+1)

Binyamin Dissen

Formerly of: SKK, Inc.
             UCCEL
             Computer Associates
             MVS Software
             GOAL Systems
             LEGENT



Thu, 28 May 1998 03:00:00 GMT  
 Search and Replace in REXX?

Quote:


>:>Hello folks;
>:>
>:>    Is there a quick and dirty way to search a file for a string and
>:>replace it with another string?
>:>
>:>    Post or E-Mail, please: I don't really know REXX, and I'm pulling my
>:>hair out.
>:>
>:>
>:>      TIA:
>:>      Mark G. Pomerleau

>:>

>How about

>      TEMP = POS(searchfor,string)
>      If  TEMP > 0  Then
>          string = LEFT(string,TEMP-1) || ,
>                   newstring || ,
>                   SUBSTR(string,TEMP+LENGTH(searchfor)+1)

>Binyamin Dissen

>Formerly of: SKK, Inc.
>             UCCEL
>             Computer Associates
>             MVS Software
>             GOAL Systems
>             LEGENT

This example is great if the string is already in memory.  I believe the question,
however, concerned a file.  If a file must be searched, a loop for file i/o using LINEIN
or CHARIN should be placed around your example.  Each read should be rewritten.  
There may be a better way but I usually write to a temp file and then rename
afterwards.  See the following:

/* REXX */
filename = 'TEST.TXT'
newfile = 'TEST.NEW'
searchfor = 'something'
newstring = 'something else'

DO WHILE LINES( filename ) > 0
   string = LINEIN( filename )

   TEMP = POS(searchfor,string)
   If  TEMP > 0  Then
       string = LEFT(string,TEMP-1) || ,
                newstring || ,
                SUBSTR(string,TEMP+LENGTH(searchfor)+1)

   CALL LINEOUT newfile, string  
END

/* Close the files */
CALL LINEOUT filename
CALL LINEOUT newfile

NOTE:  In this example it is assumed that only one occurrance per line is
expected, that the occurrance does not cross the end of a line, and that
the file is delimited by end-of-line markers.

Bryan Ellis



Fri, 29 May 1998 03:00:00 GMT  
 Search and Replace in REXX?


Quote:
> Hello folks;

>     Is there a quick and dirty way to search a file for a string and
> replace it with another string?

>     Post or E-Mail, please: I don't really know REXX, and I'm pulling my
> hair out.

There's no built-in function for this. The 'classic' file filter goes like:

/* filter InFile to OutFile replacing all InString with OutString */

  parse arg InString OutString InFile OutFile
  do while lines( InFile )
    InLine = linein( InFile )

    if pos( InString, InLine ) = 0 then
      OutLine = InLine
    else do
      parse var InLine Prefix ( InString ) Suffix
      OutLine = Prefix || OutString || Suffix
      end

    call lineout OutFile, OutLine
  end
  call lineout InFile
  call lineout OutFile

which should work with pipes/redirection since InFile & OutFile will then be
null strings (however piping doesn't work under OS/2 - pipe in works but pipe
out seems to go through the interpreter ie gets run; redirection works both
ways but has to be from/to a file). If you want OutFile to replace InFile
then add the line

 'rename' OutFile InFile

or whatever your OS requires.

Yours, Patrick
_______________________________________________________________________________


I tend to eat my UUCP feed once a day so replies can take two days

"Occam's razor is so sharp I bought the whole argument"



Sat, 30 May 1998 03:00:00 GMT  
 Search and Replace in REXX?
Perhaps the easist thing to do is to get a copy of GNUSED for OS/2 to do
Search and replaces in files.
--
jim drash



Sun, 31 May 1998 03:00:00 GMT  
 Search and Replace in REXX?

...

Quote:
> /* filter InFile to OutFile replacing all InString with OutString */

>   parse arg InString OutString InFile OutFile
>   do while lines( InFile )
>     Line = linein( InFile )

>     if pos( InString, Line ) = 0 then
>       Line = OutLine
>     else do
>       parse var Line Prefix ( InString ) Suffix
>       Line = Prefix || OutString || Suffix
>       end

or, if you want it to work properly, being able to cope with more than one
occurrence of InString in a line, and also translating eg 'a' to 'aa':

      CurPos = pos( InString, Line )
      do while CurPos > 0
        Line =    left( Line, CurPos - 1 ),
               || OutString,
               || substr( Line, CurPos + length( InString ) )
        CurPos = pos( InString, Line, CurPos + length( OutString ) )
      end

Quote:
>     call lineout OutFile, Line
>   end      
>   call lineout InFile
>   call lineout OutFile

...

Yours, Patrick
__________________________________________________________________

Patrick Herring, Primrose Hill, London, UK          
I tend to eat my UUCP feed once a day so replies can take two days



Sun, 31 May 1998 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. search and replace via rexx

2. searching and replacing a string within a file with rexx

3. Will Object REXX replace REXX

4. Rexx-search the web quickly with Search Spaniel

5. 2-file awk search-and-replace

6. in-file search/replace question without using mv or cp

7. Search and replace text in a file based on a specific line

8. awk "search and replace"

9. Search and replace

10. Searching/Replacing with special characters.

11. search replace

12. Q: VW2.0/ENVY Cross Class Search and Replace

 

 
Powered by phpBB® Forum Software