Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for cut.cmd

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

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* Attempt to "port" the Unix CUT command to Rexx  */
  3. /* Rob van der Woude, May 16 1998 - June 4 2000    */
  4. /* Usage: any_command | CUT { -C:n | -F:n          */
  5. /*        [ -D:"any_string" [ -I ] ] } [ -L:n ]    */
  6. /*        [ -S ] [ -V:varname ] [ -X:command ]     */
  7. /* * * * * * * * * * * * * * * * * * * * * * * * * */
  8.  
  9. /* Load RexxUtil */
  10. if RxFuncQuery( "SysLoadFuncs" ) <> 0 then do
  11. 	call RxFuncAdd "SysLoadFuncs", "RexxUtil", "SysLoadFuncs"
  12. 	call SysLoadFuncs
  13. end
  14.  
  15. /* Parse and check command line */
  16. parse arg cmdline
  17. cmdline = translate( cmdline, '-', '/' )
  18. parse value cmdline with cmdline'-x:'xcommand
  19. if xcommand = "" then parse value cmdline with cmdline'-X:'xcommand
  20. cmdline = strip( cmdline )
  21. parse upper value cmdline with .'-C:'column .
  22. parse       value cmdline with .'-d:"'delimiter'"'.
  23. if delimiter = '' then parse value cmdline with .'-D:"'delimiter'"'.
  24. parse upper value cmdline with .'-F:'field .
  25. parse upper value cmdline with .'-L:'length .
  26. parse upper value cmdline with .'-V:'variable .
  27. ipos = pos( ' -I', translate( cmdline ) )
  28. if ipos = 0 then do
  29. 	case      = ''
  30. end
  31. else do
  32. 	if ipos > lastpos( '"', cmdline ) | ipos < pos( '"', cmdline ) then do
  33. 		case      = 'upper'
  34. 		delimiter = translate( delimiter )
  35. 	end
  36. 	else do
  37. 		case = ''
  38. 	end
  39. end
  40. spos = pos( ' -S', translate( cmdline ) )
  41. if spos = 0 then do
  42. 	skip = 0
  43. end
  44. else do
  45. 	if spos > lastpos( '"', cmdline ) | spos < pos( '"', cmdline ) then do
  46. 		skip = 1
  47. 	end
  48. 	else do
  49. 		skip = 0
  50. 	end
  51. end
  52. select
  53. 	when column <> '' & field <> '' then call Syntax
  54. 	when length <> '' & datatype( length, 'W' ) <> 1 then call Syntax
  55. 	when field  <> '' then do
  56. 		if datatype( field,  'W' ) <> 1 then call Syntax
  57. 		cuttype = 'WORD'
  58. 		cutpos  = field
  59. 	end
  60. 	when column <> '' then do
  61. 		if delimiter <> '' then call Syntax
  62. 		if datatype( column, 'W' ) <> 1 then call Syntax
  63. 		cuttype = 'CHAR'
  64. 		cutpos  = column
  65. 	end
  66. 	otherwise call Syntax
  67. end
  68. cutlen = 0
  69. if length <> '' then cutlen = length
  70. if delimiter = '' then delimiter = ' '
  71. if variable  = '' then variable = 'CUT'
  72.  
  73. /* Purge variable */
  74. call value variable, "", "OS2ENVIRONMENT"
  75.  
  76. /* Read Standard Input */
  77. empty = 0
  78. do i = 1 by 1 while lines( ) > 0
  79. 	parse pull line.i
  80. 	if line.i = "00"X then leave
  81. 	if line.i = "1A"X then leave
  82. 	if line.i <> "" then empty = 0
  83. 	if line.i = "" then empty = empty + 1
  84. 	/* Stop after 100 empty lines */
  85. 	if empty > 100 then leave
  86. end
  87. /* Ignore those 100 empty lines */
  88. line.0 = i - 100
  89.  
  90. /* Cut lines as specified on command line */
  91. cuttot  = cutpos + cutlen
  92. do i = 1 to line.0
  93. 	if line.i = "" then do
  94. 		msg = ""
  95. 		call Output
  96. 		iterate
  97. 	end
  98. 	if cuttype = "CHAR" then do
  99. 		linelen = length( line.i )
  100. 		if linelen >= cutpos then do
  101. 			select
  102. 				when cutlen = 0 then do
  103. 					msg = substr( line.i, cutpos )
  104. 					call Output
  105. 				end
  106. 				when cutlen > 0 then do
  107. 					if linelen < cuttot then do
  108. 						msg = substr( line.i, cutpos )
  109. 						call Output
  110. 					end
  111. 					else do
  112. 						msg = substr( line.i, cutpos, cutlen )
  113. 						call Output
  114. 					end
  115. 				end
  116. 				otherwise call Syntax
  117. 			end
  118. 		end
  119. 		else do
  120. 			msg = ""
  121. 			call Output
  122. 		end
  123. 	end
  124. 	if cuttype = "WORD" & delimiter = " " then do
  125. 		linelen = words( line.i )
  126. 		if linelen >= cutpos then do
  127. 			select
  128. 				when cutlen = 0 then do
  129. 					msg = subword( line.i, cutpos )
  130. 					call Output
  131. 				end
  132. 				when cutlen > 0 then do
  133. 					if linelen < cuttot then do
  134. 						msg = subword( line.i, cutpos )
  135. 						call Output
  136. 					end
  137. 					else do
  138. 						msg = subword( line.i, cutpos, cutlen )
  139. 						call Output
  140. 					end
  141. 				end
  142. 				otherwise call Syntax
  143. 			end
  144. 		end
  145. 		else do
  146. 			msg = ""
  147. 			call Output
  148. 		end
  149. 	end
  150. 	if cuttype = "WORD" & delimiter <> " " then do
  151. 		string = line.i
  152. 		dlen = length( delimiter )
  153. 		do j = 1 by 1 until string = ""
  154. 			interpret 'parse '||case||' value string with word.'||j||'"'||delimiter||'"string'
  155. 			word.0 = j
  156. 		end
  157. 		linelen = word.0
  158. 		line = word.cutpos
  159. 		if linelen > cutpos then do j = cutpos + 1 by 1 to linelen
  160. 			line = line||delimiter||word.j
  161. 		end
  162. 		if linelen >= cutpos then do
  163. 			select
  164. 				when cutlen = 0 then msg = line
  165. 				when cutlen > 0 then do
  166. 					if linelen < cuttot then do
  167. 						msg = line
  168. 					end
  169. 					else do
  170. 						dpos = 0
  171. 						do j = 1 to cutlen
  172. 							dpos = pos( delimiter, line, dpos + dlen )
  173. 						end
  174. 						msg = substr( line, 1, dpos )
  175. 					end
  176. 				end
  177. 				otherwise call Syntax
  178. 			end
  179. 			call Output
  180. 		end
  181. 		else do
  182. 			msg = ""
  183. 			call Output
  184. 		end
  185. 	end
  186. end
  187. EXIT
  188.  
  189.  
  190. Output:
  191. 	if skip = 0 | strip( msg ) <> "" then do
  192. 		if xcommand <> "" then do
  193. 			call value variable, msg, "OS2ENVIRONMENT"
  194. 			address CMD "@CMD /C "||xcommand
  195. 		end
  196. 		else do
  197. 			say msg
  198. 		end
  199. 	end
  200. return
  201.  
  202.  
  203. Syntax: procedure
  204. 	call beep 220, 240
  205. 	say
  206. 	say " CUT,  Version 2.01 for OS/2"
  207. 	say " (C) 1998 - 2000, Rob van der Woude"
  208. 	say " http://www.robvanderwoude.com"
  209. 	say
  210. 	say " Usage:  <any_command>  |  CUT  <options>"
  211. 	say
  212. 	say " Options:               Function:                                Dependency"
  213. 	say " __________________________________________________________________________"
  214. 	say
  215. 	say "    -C:<column_number>  Parse by Columns or Characters"
  216. 	say '    -D:"<delimiter>"    Delimiter character or string             -F'
  217. 	say "    -F:<field_number>   Parse by Fields or words"
  218. 	say "    -I                  Case Insensitive delimiter (should        -D"
  219. 	say "                        be the first or last parameter)"
  220. 	say "    -L:<string_length>  Number of characters to display           -C"
  221. 	say " or -L:<fields>         Number of fields (words) to display       -F"
  222. 	say "    -V:<variable_name>  Save last result in environment variable"
  223. 	say
  224. 	say " Examples:"
  225. 	say
  226. 	say "    ECHO 1234567890 | CUT -C:4"
  227. 	say
  228. 	say '    VER | TIME | CUT -F:2 -D:":" -S -V:TIME -X:TEST.CMD'
  229. 	say "    (TEST.CMD should contain one line: ECHO Time is %TIME%)"
  230. 	say
  231. 	EXIT 1
  232. end
  233.  

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