Discussion Locked

Looking for command-line tool

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello all.
I am trying to find a command-line tool that will sort through a TXT file, and write only unique lines into another TXT file.

Here is my situation: every day in my user's login scripts, I write to a single TXT file the user name andcomputer that the user just logged into. For example, the file looks like this:

MACHINE1|BOB
MACHINE2|JOE
MACHINE3|TOM
MACHINE4|JAN
MACHINE2|JOE
MACHINE5|TIM
MACHINE1|BOB
etc.

The line in my login script is this:
ECHO %COMPUTERNAME%|%USERNAME% >>LOGFILE.TXT

Now, what I want to do is sort this file by unique records. Every day, I log into my machine, so I have a bunch of records with my login logging into my workstation in this TXT file. I want to make a summary report to just list the individual records from my log file into a single TXT file. That's all. Nothing fancy. No report headers, logos, or anything like that. This is just for me.

So in my example above, this tool would create output like this:

MACHINE1|BOBMACHINE2|JOE
MACHINE3|TOM
MACHINE4|JAN
MACHINE5|TIM

(see, the duplicate records for JOE and BOB are gone)

I DON'T want to pull this file into a database and do a unique sort on it. I don't want to pull this into a spreadsheet and sort it and discard duplicates. I just want something I can put in a BAT file to run. Just a simple command line tool that reads in a TXT file, and pulls out all unique records. No GUI, no button clicking, nothing like that.

I tried to get this to work using FOR /F command, and the SORT command, and even the LIST command (all DOS-era command line commands) to no avail.

8 replies

by joseph moore

So, does someone have a "UNIQUE.EXE" file that does what I am looking for? I need it to be a freeware tool also. I am not looking to buy something to do this. It should be out there, from the old DOS days. I've checked the Win2KProfessional Resource Kit and did not find something like this (I don't have Win2KServer Resource Kit).

Also, it has to run on Win2KPro and/or WinXP.

Anyone have some old freeware tool like this lying around?

by enserng

You could do what you list and more using Event Viewer. Sorry for the gui-based suggestion, but should more than meet your requirements.

Control panel --> Local security settings -->Local policies --> audit policy. Simply enable auditing of user log in and log out on the server.

You can filter the information, export, et cetera from Event Viewer.

Hope this helps and best of luck,

Nikk

by joseph moore

Sorry, but this is not what I am looking for.
I DO run auditing on my servers for failed logon attempts, as per security policies.
This TXT file I run of logins is just for me, so I know who is logging into what machine. I do it this way for a fewreasons. One being, I DON'T want to clog up my Security log with all of the successful logins. I keep my Security log for only failed login attempts (I then run a weekly report using the Resource Kit tool Cybersafe Log Analyst to make nice reports out of the failed login info).
So, I can see where you are coming from, but this is not the way I am doing it.
Also, yes you can sort in Event Viewer (although I personally like the freeware tool Event Log View from www.engagent.com to do sorting of Event Viewer records as you suggest; it is a much better tool for that type of thing!) but you can't just import a TXT file into it.

by indyandy

This perl script gives the required output from your example input

----------------------
use strict;
my %records;
while (<>) {$records{$_}++;}
foreach (sort keys %records) { print;}
-----------------------
If it's too much typing you couldomit lines 1 and 2 (but not lines 1 OR 2 - delete both or nothing)

usage: perl myperlfile.pl myinputfile.txt > myoutputfile.txt

Note1: It stores every line of the output file in memory before printing it so it may not scale to a zillion uniquerecords but it should be ok for most of us to use.
Note2: It maintains a count of how many times each record appears but as you didn't ask for that the script quietly ignores it.

by joseph moore

Thanks! Perfect! Works like a charm. We hav a winner here!
I added the Perl script to my BAT file, and all is well! I can now generate a TXT file with unique records!
Thanks!

This discussion is closed to new replies.