Unix shell prgrams and AI techniques

Leo de Wit leo at philmds.UUCP
Tue Jun 21 04:33:40 AEST 1988


In article <7387 at watdragon.waterloo.edu> tcjones at watdragon.waterloo.edu (Terry) writes:
>
>
>Here is a shell program I use to see who else is programming at the same time
>as me.
>---------------cut here-------------
>last > file.last
>sort < file.last > file.sorted
>cut -c1-18 < file.sorted > file.cut
>tr -d ' ' < file.cut > file.names
>cut -c19- < file.sorted > file.other.data
>paste file.other.data file.names > file.paste
>uniq +60 file.paste > file.output
>
>grep 'still logged in' file.last > temp.1
>cut -f1 -d' ' < temp.1 > file.logged.in
>sort < file.logged.in > file.sorted.logged.in
>uniq < file.sorted.logged.in > file2.output
>
>fgrep -f file2.output file.output  > answerfile.out
>
>more answerfile.out
>---------------cut here---------------
>
>This program has a lot of problems, uses a lot of CPU and memory.  It
>also prints multiple names, even though I use uniq a few times.  I would
>be really interested to get some AI techniques to improve it.

Here is a somewhat faster version (at least, it satisfies the specs):
---------------cut here---------------
who
---------------cut here---------------
or even shorter, if you have a Berkeley system:
---------------cut here---------------
w
---------------cut here---------------

who is in /bin, so it should be fairly standard. Gives you login, tty and
login time. w gives some additional information (load of system, process
that's running). As for your style, I think it can be somewhat improved.
It's hard to figure out what the program itself does, at least what it is
meant to do 8-). Quoting the first part:

>last > file.last
>sort < file.last > file.sorted
>cut -c1-18 < file.sorted > file.cut
>tr -d ' ' < file.cut > file.names
>cut -c19- < file.sorted > file.other.data
>paste file.other.data file.names > file.paste
>uniq +60 file.paste > file.output

file.output seems like a unique username/tty-sorted output of last,
with the username / tty field put behind (the reason why to put them
behind is obscure). If we take the first line (last > file.last) for
granted, the other six could be written as:

sort -u +0 -20 file.last |
sed 's/^\(....................\)\(.*\)$/\2\1/
     s/  *$//' > file.output

>grep 'still logged in' file.last > temp.1
>cut -f1 -d' ' < temp.1 > file.logged.in
>sort < file.logged.in > file.sorted.logged.in
>uniq < file.sorted.logged.in > file2.output

file2.output seems like a file containing names of users still logged in
(unique and sorted). I would prefer to write it:

sed -n -e '/still logged in/s/ .*$//p' file.last | sort -u

>fgrep -f file2.output file.output  > answerfile.out
>
>more answerfile.out

This is something like: show me the data from the first file for the
users still logged in. In that case, why not in the first place
something like

last -300 | 
sed -n -e '
300q
/still logged in/{
     s/^\(....................\)\(.*\)$/\2\1/
     s/  *$//
     p
}' |
sort -u +60 |
more

Advantages: no temp files (you still have to remove all those temp
files) and last quits after some 300 lines (could increase it). Besides
that the use of pipelines makes better use of disk space (I think).

Does this sedisfy you, Terry 8-)? As you can see, I'm a sed weirdo.
One of the better tools of Unix I think: Power, Simplicity and Velocity
(P.S.V.)

    Leo.


B.T.W. Testing this script I discovered what seems to be a bug in sort
(Ultrix 2.0).
The following two lines when fed into sort -u +15:
                Mon Jun 20 19:36   still logged inuucp      ttyic
                Mon Jun 20 18:07   still logged inleo       ttyid
generate only one line:
                Mon Jun 20 19:36   still logged inuucp      ttyic
The manual says: A missing -num argument means the end of the line,
and for u: Suppress all but one in each set of equal lines. Ignored bytes
and bytes outside keys do not participate in the comparision. 
Am I wrong or is the manual wrong (or is sort buggy)?

    Leo.



More information about the Comp.unix.questions mailing list