From: mccarthy@evetpu.enet.dec.com (Brian J. McCarthy)
Newsgroups: vmsnet.tpu
Subject: Re: How to EDIT files via a COMMAND FIle
Date: 23 Nov 92 11:35:39 GMT
Organization: Digital Equipment Corporation


In article <1ejv1aINNeqa@gap.caltech.edu>, carl@SOL1.GPS.CALTECH.EDU (Carl J Lydick) writes...
>In article <01GRDAR6Q8RM001XBD@utrcgw.utc.com>, "frank (Chip) Dyer x9568 ..Electrical Design" writes:
>> EVE/TPU experts:
>>
>> I have about 50 files with the number "100C000" (hexadecimal) in them.  I
>> need to edit all these files and replace "100C000" with "1010000."  Is it
>> possible to do this through a non-interactive (automatic) process?
>>
>> Ideally I want to have a COMMAND procedure do a GLOBLAL REPLACE on
>> "100C000" - something like
>>
>>    $ EDIT/TPU/NOINIT/NODISplay/COMmand=SYS$INPUT in.src/OUTput=OUT.SRC
>>    EVE_REPLACE("100A","*!!*!!**")
>>    $ EXIT
>>
>> Is this possible?  I have never written or used any TPU procedures, so
>> any help you can provide will greatly be appreciated.  I use EVE quite a
>> bit, but as far as writing or using TPU procedures, I have never done
>> that.
> 
>$	EDIT/TPU/NODISPLAY/NOSECTION/COMMAND=SYS$INPUT: in.src/OUTPUT=out.src
>POSITION(CREATE_BUFFER('',GET_INFO(COMMAND_LINE,'FILE_NAME')));
>LOOP	R := SEARCH("100C000", FORWARD);
>	EXITIF R=0;
>	POSITION(R);
>	ERASE(R);
>	COPY_TEXT("1010000");
>ENDLOOP;
>WRITE_FILE(CURRENT_BUFFER, GET_INFO(COMMAND_LINE,'OUTPUT_FILE'));
>EXIT;
>--------------------------------------------------------------------------------
>Carl J Lydick | INTERnet: CARL@SOL1.GPS.CALTECH.EDU | NSI/HEPnet: SOL1::CARL
> 

This works great when you know exactly what you want to replace and want to
hard code it into the command procedure.  I sent something out to info-tpu
mailing list and I will post it here.  It creates a temporary TPU command file
that senses wildcard searches.

I just did some quick hand edits to the file without checking to see if it 
still works (but it should...)

Here it is:

$! replace_wild.com
$!
$! generic replacement com file.  Changes every occurance of P3 to P4
$! found in file P1 and writes it out to P2
$!
$! If p3 (the search string) contains a wildcard character ("*"), then
$! P5 contains what characters are to be matched for that wildcard character
$! For example:
$!
$! @replace_wild <input-file> <output-file> "export : '*';" - 
$! $_           "export : 'V01-123';" "vx1234567890-"
$!
$! would cause the following strings 
$! export : '';
$! export : 'Vx001-x55';
$! export : 'V1.0';
$! to be replaced with 
$! "export : 'V01-123';"
$!
$ tmp_file = "sys$scratch:replace_wild.tmp"
$ open/write replace_cmd 'tmp_file
$ write replace_cmd "procedure global_replace (find_pat, repl_str, wild_chars);"
$ write replace_cmd "local found_wildcard,begin_str,end_str,search_pattern,r1;"
$ write replace_cmd "found_wildcard := index (find_pat, ""*"");"
$ write replace_cmd "if found_wildcard = 0"
$ write replace_cmd "then"
$ write replace_cmd "    search_pattern := find_pat;"
$ write replace_cmd "else"
$ write replace_cmd "    begin_str := SUBSTR (find_pat, 1, found_wildcard-1);
$ write replace_cmd "    end_str := SUBSTR (find_pat, found_wildcard+1);
$ !
$ ! Create a pattern that starts with everything up to the wildcard and then
$ ! spans all the characters passed in to the command procedure in p5, then 
$ ! ends with what ever follows the wildcard character
$ !
$ write replace_cmd "    search_pattern := begin_str + (SPAN (wild_chars) | """")  + end_str;"
$ write replace_cmd "endif;"
$ write replace_cmd "position (beginning_of (current_buffer));"
$ write replace_cmd "loop"
$ write replace_cmd "    r1 := search_quietly (search_pattern, forward, no_exact);"
$ write replace_cmd "    if r1 = 0 then return; endif;"
$ write replace_cmd "    position (r1);"
$ write replace_cmd "    erase (r1);"
$ write replace_cmd "    copy_text (repl_str);"
$ write replace_cmd "endloop;"
$ write replace_cmd "endprocedure;"
$ write replace_cmd "!"
$ write replace_cmd "b1 := create_buffer (""b1"", get_info(command_line,""file_name""));"
$ write replace_cmd "position (b1);"
$ write replace_cmd "global_replace (""''P3'"", ""''P4'"", ""''p5'"");"
$ write replace_cmd "write_file (b1, get_info(command_line, ""output_file""));"
$ write replace_cmd "quit (off, 1);             "
$ close replace_cmd
$ EDIT/TPU/NODISP/NOSECT/NOINIT/COMMAND='tmp_file' -
	/output='P2 'P1
$ !
$ delete 'tmp_file';
********************************************************************************
Brian J. McCarthy			DECTPU/EVE Engineering Project Leader
Digital Equipment Corporation		Nashua, NH
********************************************************************************