z/OS Using REXX and z/OS UNIX System Services
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Count newlines, words, and bytes

z/OS Using REXX and z/OS UNIX System Services
SA23-2283-00

This is an example of a REXX program that can run as a shell command or filter. It is a REXX implementation of the wc (word count) utility supporting the options -c, -w, and -l.

The program uses open, close, and EXECIO. To read standard input, it accesses the file /dev/fd0.

 
/* rexx */
parse value 'l   w   c' with,
             lcl lcw lcc .          /* init lower case access vars    */
argx=getopts('lwc')                 /* parse options                  */
if argx=0 then return 1             /* return on error                */
if opt.0=0 then                     /* no opts, set defaults          */
   parse value '1       1       1' with,
                opt.lcl opt.lcw opt.lcc .
if __argv.0>argx then                /* multiple files specified       */
   single=0
else
if __argv.0=argx then                /* one file specified             */
   single=1
else
   do                               /* no files specified, use stdin  */
   single=2
   __argv.argx='/dev/fd0'            /* handle it like fd0 specified   */
   __argv.0=argx
   end
parse value '0   0   0' with,
             twc tcc tlc .          /* clear total counters           */
address syscall
do i=argx to __argv.0                /* loop through files             */
   fi=__argv.i                       /* get file name                  */
   parse value '0  0  0' with,
                wc cc lc .          /* clear file counters            */
   'open (fi)' o_rdonly 000         /* open the file                  */
   fd=retval
   if fd=-1 then
      do                            /* open failed                    */
      say 'unable to open' fi
      iterate
      end
   do forever                       /* loop reading 1 line at a time  */
      address mvs 'execio 1 diskr' fd '(stem LN.'
      if rc<>0 | ln.0=0 then leave  /* error or end of file           */
      if opt.lcw=1 then
         wc=wc+words(ln.1)          /* count words in line            */
      if opt.lcc=1 then
         cc=cc+length(ln.1)+1       /* count chars in line + NL char  */
      if opt.lcl=1 then
         lc=lc+1                    /* count lines                    */
   end
   'close' fd                       /* close file                     */
   twc=twc+wc                       /* accumulate total words         */
   tlc=tlc+lc                       /* accumulate total lines         */
   tcc=tcc+cc                       /* accumulate total chars         */
   if opt.lcw1 then wc=''         /* format word count              */
                 else wc=right(wc,7)
   if opt.lcl<>1 then lc=''         /* format line count              */
                 else lc=right(lc,7)
   if opt.lcc<>1 then cc=''         /* format char count              */
                 else cc=right(cc,7)
   if single=2 then fi=''           /* if stdin used clear filename   */
   say lc wc cc '  'fi              /* put out counts message         */
end
if single=0 then                    /* if multiple files specified    */
   do                               /* format and output totals line  */
   if opt.lcw<>1 then twc='
                 else twc=right(twc,7)
   if opt.lcl<>1 then tlc='
                 else tlc=right(tlc,7)
   if opt.lcc<>1 then tcc='
                 else tcc=right(tcc,7)
   say tlc twc tcc '  total'
   end
return 0

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014