HP3000-L Archives

September 2002, Week 2

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Jeff Woods <[log in to unmask]>
Reply To:
Jeff Woods <[log in to unmask]>
Date:
Wed, 11 Sep 2002 01:54:22 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (81 lines)
Craig M. Lalley asked:
>What is the quickest way to determine the number of lines of code?
>i.e.  The number of lines of code in files in a group?

That's pretty easy from the POSIX shell.  All the following answers assume
you are in the shell.  To get into the shell use one of the following:

:sh
:sh -L
:sh.hpbin.sys -L
:xeq sh.hpbin.sys -L
:run sh.hpbin.sys;info="-L"

... then "cd /ACCOUNT/GROUP/" and type the "answer" below then type "exit"
(and hit <return> after each line of course) to return to the MPE CI.]

The solution depends on which of the following you mean:

How many lines are in all the files in the group?
         wc -l *

How many lines in the files that begin with "COB"?
         wc -l COB*

How many unique lines in all the files in the group?  [This would count
multiple blank lines (or anything else duplicated) of the same length just
once.]
         sort -u * | wc -l

How many lines excluding blank lines and excluding lines where the first
non-blank byte is '#' in all files ending with "Z"?
         grep -v '^ *$' *Z | grep -v '^ *#' | wc -l

A partial Rosetta Stone for the above:

         The shell expands unquoted patterns like "*", "COB*" and "*Z" to a
list of all matching file names in the current directory and inserts that
into the command.  The asterisk (*) is analgous to "@" in the LISTFILE
command.  Remember that (almost) everything in the shell is case-sensitive;
and MPE filenames are all uppercase.

         "wc" (short for "Word Count") is a program that counts bytes,
words and/or lines.  "wc -l" counts just lines.

         "sort" sorts (default key is the whole line) one or files and
writes the result to stdout unless told otherwise.  "sort -u" drops lines
with duplicate keys.

         The bar colon (|) is used to form a "pipe" where the output of the
command on the left is used as the input to the command on the right.

         "grep" (short for something like "General Regular Expression
Program", IIRC) is a very versatile string matching tool that uses "regular
expressions" (which are analagous to filename pattern matching rules, but
*far* more flexible... and not very compatible in format).  In my examples
above the regular expressions are contained between pairs of apostrophes
(single quote marks) because the shell interprets that form of quoting to
mean "don't change this, just use it exactly as shown".  In a regular
expression, asterisk means "zero or more of the preceeding atom" where an
atom is often a single character (like the blank in my examples); also "^"
and "$" mean "beginning of line" and "end of line" respectively.  The "-v"
tells grep to reverse it's normal logic and process lines that "don't
match" instead of those that "do match".

P.S.  If you think those examples are handy, you should learn about "find"
and "for" and "test" in the shell too.  :)

P.P.S.  If you decide you don't like the POSIX shell because it's slow
and/or "just feels like it's not quite integrated" then you should try a
shell like BASH or KSH on a real Unix system (like Linux, BSD or any of the
proprietary "eviscerating" Unices).  POSIX on MPE/iX is very nice... but
it's still got a lot of rough edges compared to "real" Unix.  In other
words, don't judge the Unix environment by the POSIX environment on MPE.

--
Jeff Woods
[log in to unmask]

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

ATOM RSS1 RSS2