%%s 0/0/0 %%d D 1.1 25-Mar-82 12:02:34 v1.1 1 0 %%c Version 1.1 is the Spring 1982 Distribution of the LBL/Hughes release %%c of the Software Tools Virtual Operating System software and documentation. %%T %%I 1 #-h- ch.r 3123 asc 25-mar-82 07:25:00 v1.1 (sw-tools v1.1) #-h- defns 98 asc 25-mar-82 07:24:41 v1.1 (sw-tools v1.1) ## symbol definitions for ch tool define(NEXPR,10) # maximum no of expressions on command line #-h- main 2409 asc 25-mar-82 07:24:42 v1.1 (sw-tools v1.1) ### Ch Change `string1' into `string2' DRIVER(ch) character arg(MAXARG), lin(MAXLINE), new(MAXLINE), to(MAXPAT) integer from(MAXPAT), pat(MAXPAT, NEXPR) integer addset, amatch, getarg, getlin, getpat, getsub # function(s) integer gmatch, index, itoc # function(s) integer andpat, except, frarg, i, junk, k, lastm, m, narg integer npat, status, toarg string usestr "usage: ch [-ax] [expression] ... from [to]." call query(usestr) except = NO andpat = NO narg = 0 for( i = 1 ; getarg( i, arg, MAXARG) != EOF ; i = i + 1 ) if( arg(1) == '-' ) { call strcpy( arg, lin) call fold(lin) if( index( lin, 'a') > 0 ) andpat = YES if( index( lin, 'x') > 0 ) except = YES call delarg(i) i = i - 1 } else narg = narg + 1 if( narg == 0 ) call error(usestr) else if( narg == 1 | narg == 2 ) { frarg = 1 toarg = 2 npat = 1 } else { toarg = narg frarg = narg - 1 npat = narg - 2 } if( npat > NEXPR ) { call putlin( "? Maximum number of expressions permitted is ", ERROUT) i = itoc( NEXPR, arg, MAXARG) call error(arg) } junk = getarg( frarg, arg, MAXARG) if( getpat( arg, from) == ERR ) call error( "? Illegal fromexpr pattern." ) if( getarg( toarg, arg, MAXARG) == EOF ) arg(1) = EOS if( getsub( arg, to) == ERR ) call error( "? Illegal toexpr." ) for( i = 1 ; i <= npat ; i = i + 1 ) { junk = getarg( i, arg, MAXARG) if( getpat( arg, pat( 1, i) ) == ERR ) { call putlin( "? Illegal pattern: ", ERROUT) call error(arg) } } while( getlin( lin, STDIN) != EOF ) { status = gmatch( lin, pat, npat, andpat) if( ( status == YES & except == NO ) | ( status == NO & except == YES ) ) { k = 1 lastm = 0 for( i = 1 ; lin(i) != EOS ; ) { m = amatch( lin, i, from) if( m > 0 & lastm != m ) # replace matched text { call catsub( lin, i, m, to, new, k, MAXLINE) lastm = m } if( m == 0 | m == i ) # no match or null match { junk = addset( lin(i), new, k, MAXLINE) i = i + 1 } else # skip matched text i = m } if( addset( EOS, new, k, MAXLINE) == NO ) { k = MAXLINE junk = addset( EOS, new, k, MAXLINE) call remark( "? Line truncated:." ) call putlin( new, ERROUT) call putch( '@n', ERROUT) } call putlin( new, STDOUT) } else call putlin( lin, STDOUT) } DRETURN end #-h- gmatch 397 asc 25-mar-82 07:24:44 v1.1 (sw-tools v1.1) ### GMatch Check for a match. integer function gmatch( lin, pat, elevel, andpat) character lin(ARB) integer andpat, elevel, i, pat(MAXPAT, NEXPR), status integer match # function(s) for( i = 1 ; i <= elevel ; i = i + 1 ) { status = match( lin, pat( 1, i) ) if( andpat == NO & status == YES ) return(YES) else if( andpat == YES & status == NO ) return(NO) } return(andpat) end #-h- ch.fmt 4254 asc 25-mar-82 07:25:05 v1.1 (sw-tools v1.1) .so ~bin/manhdr .hd Ch (1) 7-Apr-78 make changes in text files .sy ch [-ax] [expression] ... fromexpr [toexpr] .ds ch copies each line of the standard input to the standard output, globally substituting the text pattern "toexpr" for "fromexpr" on each line that satisfies matching criteria defined by the leading expressions "expression" and the switches. (A text pattern is a subset of a "regular expression"--see the "ed" writeup for a complete description.) Three possible courses of action are taken depending upon the number of text patterns(n) found in the command line: .sp .in +5 .ti -5 n=1 The text pattern is assumed to be "fromexpr" with a null "toexpr"; it is equivalent to the ed command .ti +10 g/fromexpr/s///g .ti -5 n=2 The first text pattern is "fromexpr", the second is "toexpr"; it is equivalent to the ed command .ti +10 g/fromexpr/s//toexpr/g .ti -5 n>=3 The (n-1)th pattern is "fromexpr", the nth is "toexpr" and patterns 1...n-2 are used to determine the lines upon which to perform the substitution. The default is that any line which matches any one of the n-2 leading expressions are eligible for substitution. If the -a flag is specified, only lines which match all n-2 leading expressions in any order are eligible. If the -x flag is specified, all lines which don't satisfy the above criteria are eligible. (See the writeup on find for more information.) In particular, if n=3, .ti +10 ch expr from to .br is equivalent to the ed command .ti +10 g/expr/s/from/to/g .ti +10 ch -x expr from to .br is equivalent to the ed command .ti +10 x/expr/s/from/to/g .sp .in -5 The substitution string "toexpr" may be a string of replacement characters, null to effect a deletion, or it may include the special "ditto" character "&" to put back the "fromexpr" string and thus effect an insertion. It may also contain the expressions `$1' ... `$9', which cause the corresponding tagged pattern in the input to be inserted. If a deletion is desired with the multiple leading tag expressions, a "toexpr" of "" -i.e. quotes around an empty string may be used. A text pattern consists of the following elements: .nf c literal character ? any character except newline % beginning of line $ end of line (null string before newline) [...] character class (any one of these characters) [!...] negated character class (all but these characters) {expr} tagged pattern (referenced by $1 ... $9) * closure (zero or more occurrences of previous pattern) + anchored closure (one or more occurrences of previous pattern) @c escaped character (e.g., @%, @[, @*) .fi Any special meaning of characters in a text pattern is lost when escaped, inside [...], or for: .nf % not at beginning $ not at end * at beginning + at beginning .fi A character class consists of zero or more of the following elements, surrounded by [ and ]: .nf c literal character a-b range of characters (digits, lower or upper case) ! negated character class if at beginning @c escaped character (@! @- @@ @]) .fi Special meaning of characters in a character class is lost when escaped or for .nf ! not at beginning - at beginning or end .fi An escape sequence consists of the character @ followed by a single character: .nf @f formfeed @l linefeed @n newline @r return @t tab @OOO the octal digit representation for an ASCII character .ti +10 for example, @001 for the ASCII character SOH @c c (including @@) .fi For a complete description, see "Software Tools" pages 135-154. Care should be taken when using the characters % $ [ ] ! * + @ and any shell characters in the text pattern. It is often necessary to enclose the entire substitution pattern in quotes. .fl none .sa The UNIX tool GRES .br The tools find and ed .br xch - extended change utility .di An error message is printed if the pattern given is illegal. .au 'CH' was originally implemented on BKY by Debbie Scherrer from Kernighan and Plauger's "Software Tools". Major modifications were performed by Joe Sventek. .bu A minus sign(dash[-]) may not start an expression. %%E 1