[net.unix] KSH substring function

alex@rruxc.UUCP (A DeSimone) (11/08/85)

In a previous posting, I asked for help on the syntax of KSH's substring
builtin.  Among others, Tom @ gamma!hoqam!twb replied (BTW, thanx Tom):
> Here is what an older manual says, hope it helps
> substring [ -lL lpat ] string [ rpat ]
> Prints the substring obtained by deleting from the  string  the
> pattern  lpat  on the left and  rpat  on the right. A pattern is
> described in File Name Generation above.  The -l option deletes the
> shortest matching pattern and the -L flag deletes the largest
> matching pattern on the left.  If  rpat  is given the
> shortest matching pattern on the left is deleted.
The impetus behind using substring is that it's builtin and so is faster than
expr.  Unfortunately, Tom also points out that substring is missing from his
latest version of KSH:
> Evidently substring has gone away in the latest ksh, maybe that's
> why it is not in your manual - It's not in the latest manual here
> either.
This makes me very uncomfortable about using it.
I have it (substring) here on a VAX (S5r2) and on my AT&T unixpc(S5r0) at home.
I would be interested to know if netters have it on their KSH (even if it's
not documented).  In particular, please email a message to me indicating what
version of UN*X you're running and whether or not your KSH has substring.
Thanks much.  I'll summarize to the net.  BTW, if anyone *knows* if substring
has officially been dropped from KSH and why, please drop me a note.

Alex
..!{ihnp4,allegra}!rruxc!alex
-- 

  Alex DeSimone @ Bell Communications Research
  ..!{ihnp4,allegra}!rruxc!alex

** Disclaimer?  I don't need no stinkin' disclaimer! **

ggs@ulysses.UUCP (Griff Smith) (11/09/85)

> > Evidently substring has gone away in the latest ksh, maybe that's
> > why it is not in your manual - It's not in the latest manual here
> > either.
> This makes me very uncomfortable about using it.
> I have it (substring) here on a VAX (S5r2) and on my AT&T unixpc(S5r0) at home.
> I would be interested to know if netters have it on their KSH (even if it's
> not documented).  In particular, please email a message to me indicating what
> version of UN*X you're running and whether or not your KSH has substring.
> Thanks much.  I'll summarize to the net.  BTW, if anyone *knows* if substring
> has officially been dropped from KSH and why, please drop me a note.
> 
> Alex
> ..!{ihnp4,allegra}!rruxc!alex
> -- 
> 
>   Alex DeSimone @ Bell Communications Research
>   ..!{ihnp4,allegra}!rruxc!alex
> 

Enough!  Substring is dead, long live substring.  It has been replaced
by the substitution characters "#" and "%".  Assuming you have done
"var=abc.def", the command "echo ${var#abc.} ${var%.def}" will print
"def abc".
-- 

Griff Smith	AT&T Bell Laboratories, Murray Hill
Phone:		(201) 582-7736
Internet:	ggs@ulysses.uucp
UUCP:		ulysses!ggs  ( {allegra|ihnp4}!ulysses!ggs )

chris@pixutl.UUCP (chris) (11/13/85)

As long as we're talking about 'substring', 'expr' has a 'substr' option
that used to be documented in V6.

It goes:
	expr substr string n m

	where n is the first character of the sub-string and m the length.

	so:		expr substr abcdefgh 2 4
	gives:		bcde

'expr' also has 'match', 'lentgh', and 'index' options that are not
documented any longer. Anyone knows why?

Chris
-- 

 Chris Bertin            :         (617) 933-7735 x2336
 Pixel Systems Inc.      :	   (800) 325-3342
 300 Wildwood street     :  {allegra|ihnp4|cbosgd|ima|genrad|amd|harvard}\
 Woburn, Ma 01801        :     !wjh12!pixel!pixutl!chris

mike@whuxl.UUCP (BALDWIN) (11/14/85)

> As long as we're talking about 'substring', 'expr' has a 'substr' option
> that used to be documented in V6.
> 
> It goes:
> 	expr substr string n m
> 
> 	where n is the first character of the sub-string and m the length.
> 
> 	so:		expr substr abcdefgh 2 4
> 	gives:		bcde
> 
> 'expr' also has 'match', 'lentgh', and 'index' options that are not
> documented any longer. Anyone knows why?

The ":" operator of expr now handles all of these cases (I've never
heard of "index"; what does it do?):

-- old --				-- new --
expr match string regexp		expr string : regexp
expr length string			expr string : '.*'
expr substr string off len		expr string : '.\{off-1\}\(.\{len\}\)'

Of course, you have to make sure that "string" isn't a valid expr
operator.  Putting an ordinary char like _ at the beginning of both
the string and the regexp will fix that.
-- 
						Michael Baldwin
						{at&t}!whuxl!mike

guy@sun.uucp (Guy Harris) (11/17/85)

> > As long as we're talking about 'substring', 'expr' has a 'substr' option
> > that used to be documented in V6.
> > ...
> > 'expr' also has 'match', 'lentgh', and 'index' options that are not
> > documented any longer. Anyone knows why?
> 
> The ":" operator of expr now handles all of these cases (I've never
> heard of "index"; what does it do?):

V6?  *V6?*  I don't remember any "expr" command in V6; the main use of
"expr" is between backquotes in the shell, and the V6 shell didn't have
backquotes (alas).  V7's "expr" had them, as did S3's; neither documented
them.  They disappeared in S5; the reason is, to quote Sun's manual page for
"expr" which *does* document them:

	BUGS
	     Note that the "match", "substr", "length", and "index"
	     operators cannot themselves be used as ordinary strings.
	     That is, the expression:

		tutorial% expr index expurgatorious length
		syntax error
		tutorial%

	     generates the 'syntax error' message as shown instead of
	     the value 1 as you might expect.

("tutorial%" is a shell prompt in the above example).

"index" is documented there as:

	index "string" "character-list"
	     reports the first position in "string" at which any one
	     of the characters in "character-list" matches a character
	     in "string".

Note that, just like the V7/4.xBSD "index" function, this is *not* the same
as the PL/I "index" function.  (One reason why I approve of the renaming of
"index" to "strchr" in S3/S5.)

> -- old --				-- new --
> expr match string regexp		expr string : regexp
> ...

"old" and "new" are somewhat misnomers here; all versions of "expr" since
V7's support the "new" method as well as the "old" one.

	Guy Harris

jsdy@hadron.UUCP (Joseph S. D. Yao) (11/28/85)

In article <3002@sun.uucp> guy@sun.uucp (Guy Harris) writes:
>> > As long as we're talking about 'substring', 'expr' has a 'substr' option
>> > that used to be documented in V6.
>V6?  *V6?*  I don't remember any "expr" command in V6; the main use of
>"expr" is between backquotes in the shell, and the V6 shell didn't have
>backquotes (alas).  V7's "expr" had them, as did S3's; ...

My first memory of "expr" is in PWB 1.0, which pre-dated V7.  It may
also have been in V6.5; I don't rightly recall.
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}