home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / mswos2.zip / search.cmd < prev    next >
OS/2 REXX Batch file  |  1994-05-17  |  1KB  |  57 lines

  1. /* Replace string1 with string2 in file, save old file in .bak */
  2. /* Note: Search is case insensitive */
  3. /* Written by Scott Maxwell May 16, 1994 */
  4.  
  5. parse arg file string1 string2
  6.  
  7. if string1 == '' then do
  8.   say "Usage: SEARCH file string1 [string2]"
  9.   say ""
  10.   say "  Searches for string1 in file case-insensitively."
  11.   say "  If string2 exists, string1 is replaced with string2"
  12.   say "  and the original file is stored in <filestem>.bak"
  13.   exit(1)
  14. end
  15.  
  16. string1 = translate(string1)
  17.  
  18. if Stream(file,'C','QUERY EXISTS') = '' then do
  19.    say file "not found."
  20.    exit(1)
  21. end
  22.  
  23. if string2 = '' then do
  24.    call RxFuncAdd 'SysFileSearch','RexxUtil','SysFileSearch'
  25.    call SysFileSearch string1,file,'line.','N'
  26.    do i = 1 to line.0
  27.       parse var line.i num contents
  28.       say num":"'09'x contents
  29.    end
  30.    exit(0)
  31. end
  32.  
  33. lp = LastPos('.',file)
  34. if lp = 0 then lp = Length(file)+1
  35. bakfile = Left(file,lp-1)'.bak'
  36.  
  37. if Stream(bakfile,'C','QUERY EXISTS') \= '' then do
  38.    call RxFuncAdd 'SysFileDelete','RexxUtil','SysFileDelete'
  39.    call SysFileDelete bakfile
  40. end
  41.  
  42. '@ren' file bakfile '> nul'
  43.  
  44. do while lines(bakfile)
  45.    l = linein(bakfile)
  46.    ltran = translate(l)
  47.    p = pos(string1,ltran)
  48.    do while p \= 0
  49.       l = Left(l,p-1)||string2||substr(l,p+length(string1))
  50.       ltran = translate(l)
  51.       p = pos(string1,ltran,p+length(string2))
  52.    end
  53.    call lineout file,l
  54. end
  55. call lineout file
  56. call lineout bakfile
  57.