home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume1 / 8711 / 11 < prev    next >
Internet Message Format  |  1990-07-13  |  4KB

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