[comp.unix.questions] Unix shell prgrams and AI techniques

tcjones@watdragon.waterloo.edu (Terry) (06/16/88)

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.

-- 
"Waterloo -  I was defeated, you won the war,
 Waterloo -  promised to love you for evermore.
 Waterloo -  couldn't escape if I wanted to.
 Waterloo -  knowing my fate is to be with you."        ABBA.

mikep@ism780c.isc.com (Michael A. Petonic) (06/18/88)

In article <7387@watdragon.waterloo.edu> tcjones@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
 >[...]

This is, of course, assuming that they aren't playing rogue.

:-)

-MikeP

leo@philmds.UUCP (Leo de Wit) (06/21/88)

In article <7387@watdragon.waterloo.edu> tcjones@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.

ok@quintus.uucp (Richard A. O'Keefe) (06/22/88)

In article <514@philmds.UUCP> leo@philmds.UUCP (L.J.M. de Wit) writes:
: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)?

Trying "sort -u +15" with just these two records on a Sun-3/50 running
SunOS 3.2, I get as output whichever of the two records came first.
There are said to be problems with the -u option of sort, so I always
avoid it.  "sort +15 | uniq" works fine.

dik@cwi.nl (Dik T. Winter) (06/22/88)

In article <514@philmds.UUCP> leo@philmds.UUCP (L.J.M. de Wit) writes:
 > 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)?
 > 
The output is according the man page for sort on BSD 4.3.  A +pos argument
takes the form +m.n where m denotes a field number and n denotes a character
within a field.  So you ask sorting by the 15th field, there are not so
many fields so all lines are equal, and -u looks at the key only.
-- 
dik t. winter, cwi, amsterdam, nederland
INTERNET   : dik@cwi.nl
BITNET/EARN: dik@mcvax

limes@sun.uucp (Greg Limes) (06/22/88)

In article <133@quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) writes:
>In article <514@philmds.UUCP> leo@philmds.UUCP (L.J.M. de Wit) writes:
>: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)?
>
>Trying "sort -u +15" with just these two records on a Sun-3/50 running
>SunOS 3.2, I get as output whichever of the two records came first.
>There are said to be problems with the -u option of sort, so I always
>avoid it.  "sort +15 | uniq" works fine.

(Good thing my flame thrower is in the shop)]

It works even better if you use +.15; that little dot between the plus
sign and the one is kind of important. 

The general form is +f.c, where f is the number of fields to skip and c
is the number of characters to skip. Beats me why you would want to
skip either fifteen characters or fifteen fields above; if you wanted to
start your sort at "still", I would use +4 instead. And yes, the -u
option does precisely what the manual claims. In the above example, of
course, all the keys were empty and therefore equal. Using "-u +4" would
sort the records, leaving one line per login name per tty.

-- Redhead [limes@sun.com]			R.T.F.M.

andy@unx1.sussex.ac.uk (Andy Clews) (07/05/88)

From article <7387@watdragon.waterloo.edu>, by tcjones@watdragon.waterloo.edu (Terry):
> 
> 
> Here is a shell program I use to see who else is programming at the same time
> as me.

...Er....wouldn't it be just a little bit quicker (but only a matter of, er,
a few paltry CPU seconds) just to type "who" or even "u"?           :-}

Andy

-- 
Andy Clews, Computing Service, Univ. of Sussex, Brighton BN1 9QN, UK
JANET: andy@unx1.sussex.ac.uk 
ARPA: andy%unx1.sussex.ac.uk@nss.cs.ucl.ac.uk
BITNET: andy%unx1.sussex.ac.uk@uk.ac