davidsen@steinmetz.UUCP (William E. Davidsen Jr) (03/15/88)
Submitted-By: "William E. Davidsen Jr" <davidsen@steinmetz.UUCP> Archive-Name: substr comp.sources.misc: Volume 2, Issue 74 Submitted-By: "William E. Davidsen Jr" <davidsen@steinmetz.UUCP> Archive-Name: substr There were some references to neat ksh tricks for substrings, and I thought that this routine would be useful. It is a LOT easier to remember than the tricks for subshell, and more portable, too. I haven't tried it in other o/s, but it should run in DOS and such. : #!/bin/sh # shar+ created from directory /usr2/davidsen/bin/src # 14:54 on Mon Mar 14, 1988 by davidsen echo 'x - substr.c (text)' sed << 'E!O!F' 's/^X//' > substr.c X#include <stdio.h> X Xstatic char SCCSid[] = {"@(#)substr.c v1.2 - 11/16/87"}; X Xmain (argc, argv) X int argc; X char *argv[]; X{ X register char *ptr; X register char ch; X int start, end; /* first and last character to extract */ X int slen; /* string length */ X X if (argc < 4) X exit (0); X start = atoi (argv[2]); X end = atoi (argv[3]); X slen = strlen(argv[1]); X if (slen == 0) exit(1); X X /* test for special values */ X if (start < 0) X start += slen + 1; X if (end == 0) X end = slen - start + 1; X else if (end < 0) X end += slen - (start - 1); X X /* validate the values */ X if (start < 1 || end < 1) X exit (1); X X ptr = argv[1] + start - 1; X while (end-- && (ch = *ptr++)) X putchar (ch); X putchar ('\n'); X exit(0); X} E!O!F newsize=`wc -c < substr.c` if [ $newsize -ne 791 ] then echo "File substr.c was $newsize bytes, 791 expected" fi echo 'x - substr.1 (text)' sed << 'E!O!F' 's/^X//' > substr.1 X'\" @(#)Documentation for the 'substr' command X'\" * * * this man page requires the 'tbl' preprocessor * * * X.TH substr 1 X.SH NAME Xsubstr - extract a substring from the input arguments X.SH SYNOPSIS Xsubstr string start_char num_of_char X.SH DESCRIPTION Xsubstr extracts characters from a string provided as the first argument, and Xwrites the characters extracted to standard output. This avoids having Xto use other proprietary methods to accomplish extraction. X.SS Special values XThe second argument is the first character to be extracted. Numbering is Xfrom one rather than zero. If the starting value is negative it is Xrelative to the last character, such as -2 means the last two characters Xin the first argument. XThe third argument is the number of characters to extract. XIf the third argument is zero, all characters right of the starting Xposition are extracted. If the length Xargument is negative, it is adjusted to end relative to the end of the Xstring. A value of -2 would end the extraction trimming the last two Xcharacters from the string. X.SH EXAMPLES XTo force an update of all SCCS files open for editing in the current Xdirectory, and display a list of changes to the user. X.in +.5i X.nf Xfor pname in p.* Xdo X name=`substr $pname 3 0` X get -p -k s.$name | diff - $name X delta s.$name Xdone X.fi X.in -.5i X.SS Table of examples X.TS Xbox; Xl c c l, l n n l. Xstart 1st col width extracted Xstring argument argument characters X_ X123456 1 4 1234 X123456 3 2 34 X123456 2 0 23456 X123456 2 -2 234 X123456 -3 1 4 X123456 -4 0 3456 X.TE X.SH WARNINGS XNo error messages are produced, but the status returned is non-zero if Xthe operation fails. Having the length requested greater than the Xcharacters available is not an error. X.SH LIMITATION XThe usage of negative numbers for the starting character and length Xis not consistant. This was done so that "-2" for a start could mean use Xthe last two characters, and "-2" for a length would strip the last two Xcharacters. X.SH AUTHOR XBill Davidsen, GE Corporate R&D Center, davidsen@crdos1.uucp X'\" For more details, see man(7), as well as man(1), manroff(1), and mmt(1) E!O!F newsize=`wc -c < substr.1` if [ $newsize -ne 2100 ] then echo "File substr.1 was $newsize bytes, 2100 expected" fi exit 0 -- bill davidsen (wedu@ge-crd.arpa) {uunet | philabs | seismo}!steinmetz!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me