Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A very simple UNIQ in CMD script

2,804 views
Skip to first unread message

Frank P. Westlake

unread,
Mar 5, 2010, 11:49:36 AM3/5/10
to
The environment can be used to create a list of unique lines from a list
of lines without case sensitivity. Here is an example which will print
unique lines from a file when given a file name on the command line:

:: UNIQ.CMD
@Echo OFF
SetLocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
REM Clear all variables beginning with '$':
For /F "tokens=1* delims==" %%a in (
'"Set "$" 2>NUL:"') Do Set "%%a="

REM Define the variables from the list (here it is a file).
REM This will use the last occurrance of the line:
For /F "delims=" %%a in (%*) Do Set "$%%a=%%a"

REM Optionally use only the first occurrance of the line:
REM For /F "delims=" %%a in (%*) Do If NOT DEFINED $%%a Set "$%%a=%%a"

REM Print the unique list:
For /F "tokens=1* delims==" %%a in ('"Set "$" 2>NUL:"') Do Echo=!%%a!
Goto :EOF

This routine can be piped to SORT for a sorted list.

Frank


foxidrive

unread,
Mar 5, 2010, 1:07:55 PM3/5/10
to

That's quite neat Frank. :-)

I have a couple of comments:
1) delayed expansion is not required or desirable
2) some lines containing = will cause a hiccough
3) I changed the command string below to a more conventional format - is
there a reason you used the format above?
4) the filesize might be a bit limited but I didn't test that.

:: UNIQ.CMD
@Echo OFF


REM Clear all variables beginning with '$':

For /F "tokens=1* delims==" %%a in ('Set $ 2^>NUL') Do Set "%%a="
REM read the file and set the variables


For /F "delims=" %%a in (%*) Do If NOT DEFINED $%%a Set "$%%a=%%a"

REM Print the unique list:

For /F "tokens=1* delims==" %%a in ('Set $ 2^>NUL') Do Echo=%%b

pause

REM Print the sorted unique list:
For /F "tokens=1* delims==" %%a in ('Set $ 2^>NUL^|sort') Do Echo=%%b

pause

--
Regards,
Mic

foxidrive

unread,
Mar 5, 2010, 1:12:32 PM3/5/10
to
On Sat, 06 Mar 2010 05:07:55 +1100, foxidrive <got...@woohoo.invalid>
wrote:

>>This routine can be piped to SORT for a sorted list.
>
>That's quite neat Frank. :-)

We failed to notice that SET $ sorts the list anyway.

> REM Print the unique list:
> For /F "tokens=1* delims==" %%a in ('Set $ 2^>NUL') Do Echo=%%b

--
Regards,
Mic

Frank P. Westlake

unread,
Mar 6, 2010, 9:15:25 AM3/6/10
to
"foxidrive" news:v9i2p5tc3vh5amgng...@4ax.com...

> We failed to notice that SET $ sorts the list anyway.

Like you, I noticed it shortly after I sent the message but I was
waiting for anoter excuse before sending a correction. You might have
noticed that I often send errors and very soon after I send corrections
to those errors. Sometimes my errors were not errors at all so my
corrections to those erroneous errors were themselves errors; then I
have to send another message correcting the erroneous correction. Words
and I are not friendly; they very often disagree with what I am saying.

"foxidrive" news:2mh2p511t8ktm9c4b...@4ax.com...

> I have a couple of comments:
> 1) delayed expansion is not required or desirable

Good correction.

> 2) some lines containing = will cause a hiccough

On mine it is a burp.

> 3) I changed the command string below to a more conventional format -
> is
> there a reason you used the format above?

I don't know what you are calling the "command string".

> 4) the filesize might be a bit limited but I didn't test that.

I considered that also. It was a simple routine and I did not try to
convey any idea that it would work in all cases. In CMD scripting it is
very often the case that routines need to be customized for the
application. When I work on a script I sometimes create routines that I
don't need so I don't spend any more time improving them, but they seem
too useful to allow them to be forgotten; so I send them here for others
to consider, use, and improve if necessary. It is nice that you thought
it worth improving. I think that with some work the hic-coughs and burps
can be removed also, making it complex; but it will be good as it is for
someone who knows that those deficiencies won't be a factor and in that
case the simple script is more desirable.

Frank


0 new messages