Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for cut.rex

(view source code of cut.rex as plain text)

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /*   Attempt to "port" the Unix CUT command to Rexx                          */
  3. /*   Rob van der Woude, May 16 1998 - January 4 2003                         */
  4. /*   Usage:                                                                  */
  5. /*   any_command | CUT { -C:n | -F:n [ -D:"any_string" [ -I ] ] } [ -L:n ]   */
  6. /*   Note: The -D switch isn't functional yet, so neither is -I              */
  7. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  8.  
  9.  
  10. /* Specify maximum number of empty lines */
  11. maxEmpty = 50
  12.  
  13. /* Initialize RexxUtil */
  14. If RxFuncQuery( "sysloadfuncs" ) <> 0 Then Do
  15. 	Call RxFuncAdd "sysloadfuncs", "RexxUtil", "sysloadfuncs"
  16. 	Call sysloadfuncs
  17. End
  18.  
  19. /* Parse and check command line */
  20. Parse arg cmdline
  21. cmdline = Translate( cmdline, '-', '/' )
  22. cmdline = strip( cmdline )
  23. Parse Upper Value cmdline With .'-C:'column .
  24. Parse       Value cmdline With .'-d:"'delimiter'"'.
  25. If delimiter = '' Then Parse Value cmdline With .'-D:"'delimiter'"'.
  26. Parse Upper Value cmdline With .'-F:'field .
  27. Parse Upper Value cmdline With .'-L:'length .
  28. ipos = Pos( ' -I', Translate( cmdline ) )
  29. If ipos = 0 Then Do
  30. 	case = ''
  31. End
  32. Else Do
  33. 	If ipos > LastPos( '"', cmdline ) | ipos < Pos( '"', cmdline ) Then Do
  34. 		case = 'Upper'
  35. 		delimiter = Translate( delimiter )
  36. 	End
  37. 	Else Do
  38. 		case = ''
  39. 	End
  40. End
  41. spos = Pos( ' -S', Translate( cmdline ) )
  42. If spos = 0 Then Do
  43. 	skip = 0
  44. End
  45. Else Do
  46. 	If spos > LastPos( '"', cmdline ) | spos < Pos( '"', cmdline ) Then Do
  47. 		skip = 1
  48. 	End
  49. 	Else Do
  50. 		skip = 0
  51. 	End
  52. End
  53. Select
  54. 	When column <> '' & field <> '' Then Call Syntax
  55. 	When length <> '' & DataType( length, 'W' ) <> 1 Then Call Syntax
  56. 	When field  <> '' Then Do
  57. 		If DataType( field,  'W' ) <> 1 Then Call Syntax
  58. 		cuttype = 'WORD'
  59. 		cutpos  = field
  60. 	End
  61. 	When column <> '' Then Do
  62. 		If delimiter <> '' Then Call Syntax
  63. 		If DataType( column, 'W' ) <> 1 Then Call Syntax
  64. 		cuttype = 'CHAR'
  65. 		cutpos  = column
  66. 	End
  67. 	Otherwise Call Syntax
  68. End
  69. cutlen = 0
  70. If length <> '' Then cutlen = length
  71. If delimiter = '' Then delimiter = ' '
  72.  
  73. /* Read Standard Input */
  74. empty = 0
  75. Do i = 1 By 1 While Lines( ) > 0
  76. 	line.i = LineIn( "STDIN" )
  77. 	If line.i = "00"X Then Leave
  78. 	If line.i = "1A"X Then Leave
  79. 	If line.i = "" Then empty = empty + 1; Else empty = 0
  80. 	/* Stop after <maxEmpty> empty lines */
  81. 	If empty > maxEmpty Then Leave
  82. End
  83. /* Ignore those empty lines */
  84. line.0 = i - empty
  85.  
  86. /* Cut lines as specified on command line */
  87. cuttot  = cutpos + cutlen
  88. Do i = 1 to line.0
  89. 	If line.i = "" Then Do
  90. 		msg = ""
  91. 		Call Output
  92. 		Iterate
  93. 	End
  94. 	If cuttype = "CHAR" Then Do
  95. 		linelen = Length( line.i )
  96. 		If linelen >= cutpos Then Do
  97. 			Select
  98. 				When cutlen = 0 Then Do
  99. 					msg = substr( line.i, cutpos )
  100. 					Call Output
  101. 				End
  102. 				When cutlen > 0 Then Do
  103. 					If linelen < cuttot Then Do
  104. 						msg = substr( line.i, cutpos )
  105. 						Call Output
  106. 					End
  107. 					Else Do
  108. 						msg = substr( line.i, cutpos, cutlen )
  109. 						Call Output
  110. 					End
  111. 				End
  112. 				Otherwise Call Syntax
  113. 			End
  114. 		End
  115. 		Else Do
  116. 			msg = ""
  117. 			Call Output
  118. 		End
  119. 	End
  120. 	If cuttype = "WORD" & delimiter = " " Then Do
  121. 		linelen = Words( line.i )
  122. 		If linelen >= cutpos Then Do
  123. 			Select
  124. 				When cutlen = 0 Then Do
  125. 					msg = SubWord( line.i, cutpos )
  126. 					Call Output
  127. 				End
  128. 				When cutlen > 0 Then Do
  129. 					If linelen < cuttot Then Do
  130. 						msg = SubWord( line.i, cutpos )
  131. 						Call Output
  132. 					End
  133. 					Else Do
  134. 						msg = SubWord( line.i, cutpos, cutlen )
  135. 						Call Output
  136. 					End
  137. 				End
  138. 				Otherwise Call Syntax
  139. 			End
  140. 		End
  141. 		Else Do
  142. 			msg = ""
  143. 			Call Output
  144. 		End
  145. 	End
  146. 	If cuttype = "WORD" & delimiter <> " " Then Do
  147. 		string = line.i
  148. 		dlen = Length( delimiter )
  149. 		Do j = 1 by 1 until string = ""
  150. 			interpret 'Parse '||case||' Value string With word.'||j||'"'||delimiter||'"string'
  151. 			word.0 = j
  152. 		End
  153. 		linelen = word.0
  154. 		line = word.cutpos
  155. 		If linelen > cutpos Then Do j = cutpos + 1 by 1 to linelen
  156. 			line = line||delimiter||word.j
  157. 		End
  158. 		If linelen >= cutpos Then Do
  159. 			Select
  160. 				When cutlen = 0 Then msg = line
  161. 				When cutlen > 0 Then Do
  162. 					If linelen < cuttot Then Do
  163. 						msg = line
  164. 					End
  165. 					Else Do
  166. 						dpos = 0
  167. 						Do j = 1 to cutlen
  168. 							dpos = Pos( delimiter, line, dpos + dlen )
  169. 						End
  170. 						msg = substr( line, 1, dpos )
  171. 					End
  172. 				End
  173. 				Otherwise Call Syntax
  174. 			End
  175. 			Call Output
  176. 		End
  177. 		Else Do
  178. 			msg = ""
  179. 			Call Output
  180. 		End
  181. 	End
  182. End
  183.  
  184. /* Normal program end */
  185. Exit 0
  186.  
  187.  
  188. Output:
  189. 	If skip = 0 | strip( msg ) <> "" Then Say msg
  190. Return
  191.  
  192.  
  193. Syntax: procedure
  194. 	Call beep 220, 240
  195. 	Say
  196. 	Say "Cut.rex,  Version 0.50 beta for Regina Rexx"
  197. 	Say 'Attempt to "port" the Unix CUT command to Rexx'
  198. 	Say
  199. 	Say "Usage:  <any_command>  |  CUT  <options>"
  200. 	Say
  201. 	Say "Options:               Function:                                Dependency"
  202. 	Say "__________________________________________________________________________"
  203. 	Say
  204. 	Say "   -C:<column_number>  Parse by Columns or Characters"
  205. /*
  206. 	Say '   -D:"<delimiter>"    Delimiter character or string             -F'
  207. */
  208. 	Say "   -F:<field_number>   Parse by Fields or words"
  209. /*
  210. 	Say "   -I                  Case Insensitive delimiter (should        -D"
  211. */
  212. 	Say "                       be the first or last parameter)"
  213. 	Say "   -L:<string_length>  Number of characters to display           -C"
  214. 	Say "or -L:<fields>         Number of fields (words) to display       -F"
  215. 	Say
  216. 	Say "Examples:"
  217. 	Say
  218. 	Say "   ECHO 1234567890 | CUT -C:4"
  219. 	Say "   VER | DATE | REGINA CUT.REX -F:6"
  220. 	Say "   VER | DATE | REGINA CUT.REX -F:6 | REGINA CUT.REX -C:7"
  221. 	Say
  222. 	Say "Written by Rob van der Woude"
  223. 	Say "http://www.robvanderwoude.com"
  224. 	Exit 1
  225. Return
  226.  

page last modified: 2024-04-16; loaded in 0.0404 seconds