experiences with awk and uniq - Copyright © 1995 - 2001 John Meister

In trying to determine which users have logged into a system I tried variations of "uniq" to 
break down the list from "last".  However, it's not as easy as I thought.  It seems that uniq doesn't 
always work as expected.  Variations in distributions and a failure to RTM may be the issue.

The goal is to reduce the output from last to a single line of user information.  So far the following is true:

if I do a "last | wc -l", I get 119 occurrences on a particular system.
if I do a "last | grep -v root | wc -l", I get 88 occurrences.
if I do a "last | uniq -d | wc -l", I get 0.   (???)
if I do a "last | awk '{print $1}' | grep -v root | wc -l", I get 88.

and then, if I do:

 

last | awk '{print $1}' | grep -v root | uniq -d | wc -l

I get 12... with 10 of those being duplicates, and another 2, one of those being a duplicate. THEN, if I do:

last | awk '{print $1}' | grep -v root | grep -v reboot | uniq -d | wc -l

and didn't get the same answer!!! so much for a "uniq" afternoon.... :) It's close enough for what I'm looking for though... wait... I'll try using it with sort...

last | awk '{print $1}' | grep -v root | grep -v reboot | sort | uniq -d

AHA!, it works, while the other "uniq" without "sort" doesn't, see: >--> last | awk '{print $1}' | grep -v root | grep -v reboot | uniq -d jxxxxxx jxxxxxx ----------------------------------------------------- root@wxxxxxxx [/root] >--> last | awk '{print $1}' | grep -v root | grep -v reboot | sort | uniq -d cxxxxxxx jxxxxxxx so, now I can take this string and run it through my for loop and remotely check each workstation for user id so I can update my password file with only those users that have accessed my systems. :) #!/bin/sh # # 99dec17 - get user names from last # echo " executing script to determine users on all HP engineering workstations " for x in `cat /raid/02d/sa/rem/hostseng` do echo $x >> /raid/02d/sa/rem/log.`/usr/bin/date +%y%b%d`.users echo " ================== " >> /raid/02d/sa/rem/log.`/usr/bin/date +%y%b%d`.users echo $x remsh $x last | awk '{print $1}' | grep -v root | grep -v reboot | sort | uniq -d \ | tee -a /raid/02d/sa/rem/log.`/usr/bin/date +%y%b%d`.users echo " ================== " echo " ================== " >> /raid/02d/sa/rem/log.`/usr/bin/date +%y%b%d`.users done echo " completed list, results found in /raid/02d/sa/rem/log.`/usr/bin/date +%y%b%d`.users "


ebook:
Full Size
Jeep Buyer's
Guide
ebook:
Practical Suggestions
for Microsoft
Windows
Linux Tackles Microsoft
12 hour Video Course by john:
The Art of Linux System Administration
published by O'Reilly Media
Study Guide for the LPIC-2 Certification Exams
search for:
on the internet, or:
JohnMeister.com-fotos
LinuxMeister-Linux
BibleTech-
Bible overview


An overview of Biblical history:
"Promises and Prophets"

Wagoneers

FULL SIZE JEEPS

JeepMeister
"Jeep is America's
only real sports car."
-Enzo Ferrari


MeisterTech
Diesels +

One Page Overview of Linux Commands

click for an image of the 5 essential Linux commands

An Intro to Linux
AMSOIL product guide,
or, AMSOIL web, or 1-800-956-5695,
use customer #283461

Amsoil dealer since 1983
purchase AMSOIL
at Midway Auto on SR9 in Snohomish,
or at Northland Diesel in Bellingham, WA


SJ - 1962-1991

XJ - 1984-2001

WJ - 1999-2004

KJ - 2002-2007

WK - 2005-2010

Find the recommended
AMSOIL synthetics
for your Jeep

CJ-10A - 1984-1986

Jeepsters

MJ - 1984-1992

Willys - 1946-1965

Other Jeeps (FC)