|
Dave,
Maybe it is worth trying to step through the input record one byte at a time
without creating the string chunks and to overprint _infile_ with the
replacements as you go? Something along those lines:
data _null_ ;
file 'h:\sasdata.txt' ;
put '111-222--333-111333--666-222' ;
put '1029384722237633310983330666' ;
put '111111112222222233333333****' ;
run ;
proc format ;
invalue $sub
111 = aaa
222 = bbb
333 = ccc
other = .
;
run ;
data _null_ ;
retain replen 3 ; *default or replacement length ;
retain contig 1 ; *contiguous replacement needed ;
infile 'h:\sasdata.txt' length=len ;
input @ ;
put _infile_ @ ;
do p = 1 to len - replen + 1 ;
input @p ccc $sub. @ ;
if ccc eq: '.' then continue ;
put @p ccc @ ;
p = p + (replen - 1) * not contig ;
end ;
put ;
run ;
Kind regards,
=====================
Paul M. Dorfman
Jacksonville, FL
=====================
> -----Original Message-----
> From: David L. Ward [mailto:dward@SASHELP.COM]
> Sent: Tuesday, March 05, 2002 10:41 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Replacing a string in a file
>
>
> Hi all, before I spend too much time on this, what is the best way to
> replace all occurrences of a string with another in a binary
> file? Assume
> we want v6 compatibility (200 char string limit). The
> complication that
> arises is that if you read the binary file in 200 character
> chunks, the
> boundary of your chunks may occur in the middle of the search
> string. Plus,
> if your chunk is comprised of too many search strings, the
> replacements (if
> longer than the search strings) may make the length of the
> string longer
> than 200 characters.
>
> The file needs to be binary so don't assume I can solve this by doing
> line-based reading and writing. I'm trying to replace
> strings in a variety
> of file formats, PCL, RTF (which can be line based, I know,
> but I'd rather
> stick with an exact copy of the file just with the replacements), etc.
> Besides, binary data is like reading one LONG line.
>
> This is the blind-man's approach. There are many problems I
> won't point out
> but I'm sure they will be obvious to most of you.
>
> data _null_;
> length chunk $200;
> infile 'c:\temp\temp.txt' recfm=N;
> file 'c:\temp\temp2.txt' recfm=N;
> input chunk $char100.;
> chunk=tranwrd(chunk,'Default','Replacement');
> put chunk $char100.;
> run;
>
> Thanks for any help!
>
> David W
>
>
Blue Cross Blue Shield of Florida, Inc., and its subsidiary and
affiliate companies are not responsible for errors or omissions in this e-mail message. Any personal comments made in this e-mail do not reflect the views of Blue Cross Blue Shield of Florida, Inc.
|