substr/ 755 243 12 0 4404105643 5336 substr/SharFile.Headers 644 243 12 1141 4404105645 10414 === substr.shar =========================
Article 248 of comp.sources.misc:
Path: brl-smoke!brl-adm!umd5!rutgers!sri-spam!ames!necntc!ncoast!allbery
From: davidsen@steinmetz.UUCP (William E. Davidsen Jr)
Newsgroups: comp.sources.misc
Subject: substr - a substring extractor for shell scripts
Date: 18 Nov 87 00:54:35 GMT
Keywords: substring, shell
Approved: allbery@ncoast.UUCP
X-Archive: comp.sources.misc/8711/11
Some years ago I wrote this to allow a portable of extracting substrings
from expressions other than by use of awk. I finally got around to
writing a man page for it, and I hope it's useful.
substr/substr.1 644 243 12 4064 4404105641 7031 '\" @(#)Documentation for the 'substr' command
'\" * * * this man page requires the 'tbl' preprocessor * * *
.TH substr 1
.SH NAME
substr - extract a substring from the input arguments
.SH SYNOPSIS
substr string start_char num_of_char
.SH DESCRIPTION
substr extracts characters from a string provided as the first argument, and
writes the characters extracted to standard output. This avoids having
to use other proprietary methods to accomplish extraction.
.SS Special values
The second argument is the first character to be extracted. Numbering is
from one rather than zero. If the starting value is negative it is
relative to the last character, such as -2 means the last two characters
in the first argument.
The third argument is the number of characters to extract.
If the third argument is zero, all characters right of the starting
position are extracted. If the length
argument is negative, it is adjusted to end relative to the end of the
string. A value of -2 would end the extraction trimming the last two
characters from the string.
.SH EXAMPLES
To force an update of all SCCS files open for editing in the current
directory, and display a list of changes to the user.
.in +.5i
.nf
for pname in p.*
do
name=`substr $pname 3 0`
get -p -k s.$name | diff - $name
delta s.$name
done
.fi
.in -.5i
.SS Table of examples
.TS
box;
l c c l, l n n l.
start 1st col width extracted
string argument argument characters
_
123456 1 4 1234
123456 3 2 34
123456 2 0 23456
123456 2 -2 234
123456 -3 1 4
123456 -4 0 3456
.TE
.SH WARNINGS
No error messages are produced, but the status returned is non-zero if
the operation fails. Having the length requested greater than the
characters available is not an error.
.SH LIMITATION
The usage of negative numbers for the starting character and length
is not consistant. This was done so that "-2" for a start could mean use
the last two characters, and "-2" for a length would strip the last two
characters.
.SH AUTHOR
Bill Davidsen, GE Corporate R&D Center, davidsen@crdos1.uucp
'\" For more details, see man(7), as well as man(1), manroff(1), and mmt(1)
substr/substr.c 644 243 12 1427 4404105643 7115 #include
static char SCCSid[] = {"@(#)substr.c v1.2 - 11/16/87"};
main (argc, argv)
int argc;
char *argv[];
{
register char *ptr;
register char ch;
int start, end; /* first and last character to extract */
int slen; /* string length */
if (argc < 4)
exit (0);
start = atoi (argv[2]);
end = atoi (argv[3]);
slen = strlen(argv[1]);
if (slen == 0) exit(1);
/* test for special values */
if (start < 0)
start += slen + 1;
if (end == 0)
end = slen - start + 1;
else if (end < 0)
end += slen - (start - 1);
/* validate the values */
if (start < 1 || end < 1)
exit (1);
ptr = argv[1] + start - 1;
while (end-- && (ch = *ptr++))
putchar (ch);
putchar ('\n');
exit(0);
}
nt is zero, all characters right of the starting
position are extracted. If the length
argument is negative, it is adjusted to end relative to the end of the
string. A value of -2 would end the extraction trimming the last two
charac