home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume02 / substr < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  4.6 KB

  1. From mipos3!omepd!littlei!uunet!husc6!necntc!ncoast!allbery Fri Mar 18 19:51:47 PST 1988
  2. Article 327 of comp.sources.misc:
  3. Path: td2cad!mipos3!omepd!littlei!uunet!husc6!necntc!ncoast!allbery
  4. From: davidsen@steinmetz.UUCP (William E. Davidsen Jr)
  5. Newsgroups: comp.sources.misc
  6. Subject: v02i074: a command line substring function
  7. Keywords: substring
  8. Message-ID: <9930@steinmetz.steinmetz.UUCP>
  9. Date: 14 Mar 88 21:23:12 GMT
  10. Sender: allbery@ncoast.UUCP
  11. Reply-To: davidsen@steinmetz.UUCP (William E. Davidsen Jr)
  12. Organization: GE Corp. R & D, Schenectady,NY
  13. Lines: 132
  14. Approved: allbery@ncoast.UUCP
  15. comp.sources.misc: Volume 2, Issue 74
  16. Submitted-By: "William E. Davidsen Jr" <davidsen@steinmetz.UUCP>
  17. Archive-Name: substr
  18.  
  19. comp.sources.misc: Volume 2, Issue 74
  20. Submitted-By: "William E. Davidsen Jr" <davidsen@steinmetz.UUCP>
  21. Archive-Name: substr
  22.  
  23.   There were some references to neat ksh tricks for substrings, and I
  24. thought that this routine would be useful. It is a LOT easier to
  25. remember than the tricks for subshell, and more portable, too. I haven't
  26. tried it in other o/s, but it should run in DOS and such.
  27.  
  28. :
  29. #!/bin/sh
  30. # shar+ created from directory /usr2/davidsen/bin/src
  31. # 14:54 on Mon Mar 14, 1988 by davidsen
  32. echo 'x - substr.c (text)'
  33. sed << 'E!O!F' 's/^X//' > substr.c
  34. X#include <stdio.h>
  35. X
  36. Xstatic char SCCSid[] = {"@(#)substr.c v1.2 - 11/16/87"};
  37. X
  38. Xmain (argc, argv)
  39. X    int  argc;
  40. X    char *argv[];
  41. X{
  42. X    register char *ptr;
  43. X    register char ch;
  44. X    int start, end;    /* first and last character to extract */
  45. X    int slen;        /* string length */
  46. X
  47. X    if (argc < 4)
  48. X    exit (0);
  49. X    start = atoi (argv[2]);
  50. X    end = atoi (argv[3]);
  51. X    slen = strlen(argv[1]);
  52. X    if (slen == 0) exit(1);
  53. X
  54. X    /* test for special values */
  55. X    if (start < 0)
  56. X        start += slen + 1;
  57. X    if (end == 0)
  58. X        end = slen - start + 1;
  59. X    else if (end < 0)
  60. X        end += slen - (start - 1);
  61. X
  62. X    /* validate the values */
  63. X    if (start < 1 || end < 1)
  64. X    exit (1);
  65. X
  66. X    ptr = argv[1] + start - 1;
  67. X    while (end-- && (ch = *ptr++))
  68. X    putchar (ch);
  69. X    putchar ('\n');
  70. X    exit(0);
  71. X}
  72. E!O!F
  73. newsize=`wc -c < substr.c`
  74. if [ $newsize -ne 791 ]
  75. then echo "File substr.c was $newsize bytes, 791 expected"
  76. fi
  77. echo 'x - substr.1 (text)'
  78. sed << 'E!O!F' 's/^X//' > substr.1
  79. X'\" @(#)Documentation for the 'substr' command
  80. X'\" * * * this man page requires the 'tbl' preprocessor * * *
  81. X.TH substr 1
  82. X.SH NAME
  83. Xsubstr - extract a substring from the input arguments
  84. X.SH SYNOPSIS
  85. Xsubstr string start_char num_of_char
  86. X.SH DESCRIPTION
  87. Xsubstr extracts characters from a string provided as the first argument, and
  88. Xwrites the characters extracted to standard output. This avoids having
  89. Xto use other proprietary methods to accomplish extraction.
  90. X.SS Special values
  91. XThe second argument is the first character to be extracted. Numbering is
  92. Xfrom one rather than zero. If the starting value is negative it is
  93. Xrelative to the last character, such as -2 means the last two characters
  94. Xin the first argument.
  95. XThe third argument is the number of characters to extract.
  96. XIf the third argument is zero, all characters right of the starting
  97. Xposition are extracted. If the length
  98. Xargument is negative, it is adjusted to end relative to the end of the
  99. Xstring. A value of -2 would end the extraction trimming the last two
  100. Xcharacters from the string.
  101. X.SH EXAMPLES
  102. XTo force an update of all SCCS files open for editing in the current
  103. Xdirectory, and display a list of changes to the user.
  104. X.in +.5i
  105. X.nf
  106. Xfor pname in p.*
  107. Xdo
  108. X name=`substr $pname 3 0`
  109. X get -p -k s.$name | diff - $name
  110. X delta s.$name
  111. Xdone
  112. X.fi
  113. X.in -.5i
  114. X.SS Table of examples
  115. X.TS
  116. Xbox;
  117. Xl c c l, l n n l.
  118. Xstart    1st col    width    extracted
  119. Xstring    argument    argument    characters
  120. X_
  121. X123456    1    4    1234
  122. X123456    3    2    34
  123. X123456    2    0    23456
  124. X123456    2    -2    234
  125. X123456    -3    1    4
  126. X123456    -4    0    3456
  127. X.TE
  128. X.SH WARNINGS
  129. XNo error messages are produced, but the status returned is non-zero if
  130. Xthe operation fails. Having the length requested greater than the
  131. Xcharacters available is not an error.
  132. X.SH LIMITATION
  133. XThe usage of negative numbers for the starting character and length
  134. Xis not consistant. This was done so that "-2" for a start could mean use
  135. Xthe last two characters, and "-2" for a length would strip the last two
  136. Xcharacters. 
  137. X.SH AUTHOR
  138. XBill Davidsen, GE Corporate R&D Center, davidsen@crdos1.uucp
  139. X'\" For more details, see man(7), as well as man(1), manroff(1), and mmt(1)
  140. E!O!F
  141. newsize=`wc -c < substr.1`
  142. if [ $newsize -ne 2100 ]
  143. then echo "File substr.1 was $newsize bytes, 2100 expected"
  144. fi
  145. exit 0
  146.  
  147. -- 
  148.     bill davidsen        (wedu@ge-crd.arpa)
  149.   {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
  150. "Stupidity, like virtue, is its own reward" -me
  151.  
  152.  
  153.