home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / substr < prev    next >
Internet Message Format  |  1991-08-07  |  4KB

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