/* * * * * * * * * * * * * * * * * * * * * * * * * */ /* Attempt to "port" the Unix CUT command to Rexx */ /* Rob van der Woude, May 16 1998 - June 4 2000 */ /* Usage: any_command | CUT { -C:n | -F:n */ /* [ -D:"any_string" [ -I ] ] } [ -L:n ] */ /* [ -S ] [ -V:varname ] [ -X:command ] */ /* * * * * * * * * * * * * * * * * * * * * * * * * */ /* Load RexxUtil */ if RxFuncQuery( "SysLoadFuncs" ) <> 0 then do call RxFuncAdd "SysLoadFuncs", "RexxUtil", "SysLoadFuncs" call SysLoadFuncs end /* Parse and check command line */ parse arg cmdline cmdline = translate( cmdline, '-', '/' ) parse value cmdline with cmdline'-x:'xcommand if xcommand = "" then parse value cmdline with cmdline'-X:'xcommand cmdline = strip( cmdline ) parse upper value cmdline with .'-C:'column . parse value cmdline with .'-d:"'delimiter'"'. if delimiter = '' then parse value cmdline with .'-D:"'delimiter'"'. parse upper value cmdline with .'-F:'field . parse upper value cmdline with .'-L:'length . parse upper value cmdline with .'-V:'variable . ipos = pos( ' -I', translate( cmdline ) ) if ipos = 0 then do case = '' end else do if ipos > lastpos( '"', cmdline ) | ipos < pos( '"', cmdline ) then do case = 'upper' delimiter = translate( delimiter ) end else do case = '' end end spos = pos( ' -S', translate( cmdline ) ) if spos = 0 then do skip = 0 end else do if spos > lastpos( '"', cmdline ) | spos < pos( '"', cmdline ) then do skip = 1 end else do skip = 0 end end select when column <> '' & field <> '' then call Syntax when length <> '' & datatype( length, 'W' ) <> 1 then call Syntax when field <> '' then do if datatype( field, 'W' ) <> 1 then call Syntax cuttype = 'WORD' cutpos = field end when column <> '' then do if delimiter <> '' then call Syntax if datatype( column, 'W' ) <> 1 then call Syntax cuttype = 'CHAR' cutpos = column end otherwise call Syntax end cutlen = 0 if length <> '' then cutlen = length if delimiter = '' then delimiter = ' ' if variable = '' then variable = 'CUT' /* Purge variable */ call value variable, "", "OS2ENVIRONMENT" /* Read Standard Input */ empty = 0 do i = 1 by 1 while lines( ) > 0 parse pull line.i if line.i = "00"X then leave if line.i = "1A"X then leave if line.i <> "" then empty = 0 if line.i = "" then empty = empty + 1 /* Stop after 100 empty lines */ if empty > 100 then leave end /* Ignore those 100 empty lines */ line.0 = i - 100 /* Cut lines as specified on command line */ cuttot = cutpos + cutlen do i = 1 to line.0 if line.i = "" then do msg = "" call Output iterate end if cuttype = "CHAR" then do linelen = length( line.i ) if linelen >= cutpos then do select when cutlen = 0 then do msg = substr( line.i, cutpos ) call Output end when cutlen > 0 then do if linelen < cuttot then do msg = substr( line.i, cutpos ) call Output end else do msg = substr( line.i, cutpos, cutlen ) call Output end end otherwise call Syntax end end else do msg = "" call Output end end if cuttype = "WORD" & delimiter = " " then do linelen = words( line.i ) if linelen >= cutpos then do select when cutlen = 0 then do msg = subword( line.i, cutpos ) call Output end when cutlen > 0 then do if linelen < cuttot then do msg = subword( line.i, cutpos ) call Output end else do msg = subword( line.i, cutpos, cutlen ) call Output end end otherwise call Syntax end end else do msg = "" call Output end end if cuttype = "WORD" & delimiter <> " " then do string = line.i dlen = length( delimiter ) do j = 1 by 1 until string = "" interpret 'parse '||case||' value string with word.'||j||'"'||delimiter||'"string' word.0 = j end linelen = word.0 line = word.cutpos if linelen > cutpos then do j = cutpos + 1 by 1 to linelen line = line||delimiter||word.j end if linelen >= cutpos then do select when cutlen = 0 then msg = line when cutlen > 0 then do if linelen < cuttot then do msg = line end else do dpos = 0 do j = 1 to cutlen dpos = pos( delimiter, line, dpos + dlen ) end msg = substr( line, 1, dpos ) end end otherwise call Syntax end call Output end else do msg = "" call Output end end end EXIT Output: if skip = 0 | strip( msg ) <> "" then do if xcommand <> "" then do call value variable, msg, "OS2ENVIRONMENT" address CMD "@CMD /C "||xcommand end else do say msg end end return Syntax: procedure call beep 220, 240 say say " CUT, Version 2.01 for OS/2" say " (C) 1998 - 2000, Rob van der Woude" say " http://www.robvanderwoude.com" say say " Usage: | CUT " say say " Options: Function: Dependency" say " __________________________________________________________________________" say say " -C: Parse by Columns or Characters" say ' -D:"" Delimiter character or string -F' say " -F: Parse by Fields or words" say " -I Case Insensitive delimiter (should -D" say " be the first or last parameter)" say " -L: Number of characters to display -C" say " or -L: Number of fields (words) to display -F" say " -V: Save last result in environment variable" say say " Examples:" say say " ECHO 1234567890 | CUT -C:4" say say ' VER | TIME | CUT -F:2 -D:":" -S -V:TIME -X:TEST.CMD' say " (TEST.CMD should contain one line: ECHO Time is %TIME%)" say EXIT 1 end