cmd.net: uniq

June 27, 2011

Finally the command that prompted the creation of cmd.net in the first place: uniq.

Before implementing the command, I had to find out which options the default Linux implementations provided.

I used these pages as guideline:

The resulting command filters unique lines from stdin (or a piped input file) to stdout, and supports these options:

uniq [parameters]

displays unique lines in sorted file

-u      unique lines only
-d      repeated lines only
-c      display line count
-i      ignore case
-f      [ignore fields]         ignore number of fields
-s      [skip characters]       skip number of characters
-w      [compare characters]    compare number of characters
-in     [encoding]      input encoding
-ci     [culture]       culture info
-out    [encoding]      output encoding

uniq 0.10.4117.37457
cmd.net 0.10.0.0 (c) by devio.at 2011

Note that uniq only analyzes subsequent lines of text in the file. Thus it only operates correctly on a pre-sorted file. If the input is not sorted, equal lines may be output more than once (but not subsequently).

The first version of cmd.net is available for download here, and contains the commands described in this post.


cmd.net: tee, xslt

April 18, 2011

The tee command of the cmd.net collection is a .Net implementation of the tee command available on other platforms.

As usual, the -in and -out switches define the encodings of stdin and stdout.

The filename parameter is given directly (without a leading switch), and the file’s encoding can be defined using the -enc switch.

The -a switch appends to an existing file instead of creating a new file.

The xslt command is a translation of the PowerShell xslt script I posted some time ago.

The xml file is provided on stdin, and the xslt file is defined via the -xslt switch (along with -xsltenc for the encoding).

Again, the -in and -out switches define the encodings of stdin and stdout.

The current version of cmd.net is available for download here.


cmd.net: df, enc, guid

April 15, 2011

The df command of the cmd.net collection lists all available drives and their total space, used and free space in GB.

Drive Total  Used  Free Used% FS     Label
C:\      97    57    39   59% NTFS
D:\     833   443   389   53% NTFS
E:\       0     0     0     %

(e: being a removable disk drive in this case)

The guid command writes a new GUID to stdout

D:\projects\cmdnet\net40>guid
d9999ffd-659d-4f56-a38e-698005ed3a9b

The enc command converts an encoded stdin to a different encoding on stdout using the -in [encoding] and -out [encoding] command-line switches.

enc -in [CP number or encoding name]
    -out [CP number or encoding name]
    < [input filename]
    > [output filename]
some-batch-command 
    | enc -out [CP number or encoding name] 
    > [output filename]

The current version of cmd.net is available for download here.


cmd.net: datetime

April 14, 2011

The datetime command of the cmd.net collection outputs the current date and time.

Using the -f (format) switch to select a .Net date format string (standard, custom), you can adjust the output format:

D:\projects\cmdnet\net40>datetime
14.04.2011 16:11

D:\projects\cmdnet\net40>datetime -f D
Donnerstag, 14. April 2011

D:\projects\cmdnet\net40>datetime -f T
16:15:24

D:\projects\cmdnet\net40>datetime -f o
2011-04-14T16:15:02.9943652+02:00

D:\projects\cmdnet\net40>datetime -f u
2011-04-14 16:15:15Z

Use the -utc switch to retrieve the UTC timestamp:

D:\projects\cmdnet\net40>datetime -f o -utc
2011-04-14T14:16:13.3573897Z

The -ci switch selects a CultureInfo to display the date in a different language:

D:\projects\cmdnet\net40>datetime -ci fr -f D
jeudi 14 avril 2011

D:\projects\cmdnet\net40>datetime -ci fr-fr -f D
jeudi 14 avril 2011

(I noticed that the language-only culture info only works in .Net 4)

If you want to display the date string in a language that is not supported in the DOS code page, you need to first switch the code page using the chcp command:

D:\projects\cmdnet\net40>chcp
Aktive Codepage: 850.

D:\projects\cmdnet\net40>netenv
input code page: 850
input Windows code page: 1252
input encoding: Western European (DOS)
output code page: 850
output Windows code page: 1252
output encoding: Western European (DOS)
culture: de-AT
LCID: 3079
culture display name: German (Austria)

D:\projects\cmdnet\net40>chcp 1251
Aktive Codepage: 1251.

D:\projects\cmdnet\net40>datetime -ci ru -f D
14 апреля 2011 г.

D:\projects\cmdnet\net40>chcp 850
Aktive Codepage: 850.

The current version of cmd.net is available for download here.


cmd.net: Querying the .Net Environment

April 12, 2011

As described in my introductory post on cmd.net, the utilities contained in the collection should make use of .Net functionality wherever possible.

The utilities provide command-line switches to select the encoding of the input and output streams (-in for stdin or Console.In, and -out for stdout or Console.Out), and the culture info (-ci) for NLS-sensitive operations.

How to find out the valid values for these switches?

netenv

The netenv utility displays the current input and output stream encodings and code pages, as well as the current culture:

input code page: 850
input Windows code page: 1252
input encoding: Western European (DOS)
output code page: 850
output Windows code page: 1252
output encoding: Western European (DOS)
culture: de-AT
LCID: 3079
culture display name: German (Austria)

Changing the culture info to English using the -ci switch

netenv -ci en-us

generates this output

input code page: 850
input Windows code page: 1252
input encoding: Western European (DOS)
output code page: 850
output Windows code page: 1252
output encoding: Western European (DOS)
culture: en-US
LCID: 1033
culture display name: English (United States)

listci

To query all culture infos implemented in .Net, use the listci command. This will display the LCID, name, type, and display name of all CultureInfos.

listci | find " en"

returns

    9 en          N W   English
 1033 en-US        SW   English (United States)
 2057 en-GB        SW   English (United Kingdom)
 3081 en-AU        SW   English (Australia)
 4105 en-CA        SW   English (Canada)
 5129 en-NZ        SW   English (New Zealand)
 6153 en-IE        SW   English (Ireland)
 7177 en-ZA        SW   English (South Africa)
 8201 en-JM        SW   English (Jamaica)
 9225 en-029       SW   English (Caribbean)
10249 en-BZ        SW   English (Belize)
11273 en-TT        SW   English (Trinidad and Tobago)
12297 en-ZW        SW   English (Zimbabwe)
13321 en-PH        SW   English (Republic of the Philippines)
16393 en-IN        SW   English (India)
17417 en-MY        SW   English (Malaysia)
18441 en-SG        SW   English (Singapore)

The list is sorted by the LCID by default, and can be changed using the -o switch. The -t switch restricts the list to certain CultureTypes.

listenc

To query all encodings implemented in .Net, use the listenc command. This will display the code page, name, and display name of all Encodings.

listenc | find "Unicode"

returns

 1200 utf-16                  Unicode
 1201 utf-16BE                Unicode (Big-Endian)
12000 utf-32                  Unicode (UTF-32)
12001 utf-32BE                Unicode (UTF-32 Big-Endian)
65000 utf-7                   Unicode (UTF-7)
65001 utf-8                   Unicode (UTF-8)

The list ist sorted by the code page (CP) by default, and can be changed using the -o switch.

netver

The command netver lists all installed versions of the .Net framework.

.Net Framework Versions
v2.0.50727: Version 2.0.50727.4927 SP 2 installed
v2.0.50727/1031: Version 2.0.50727.4927 SP 2 installed
v3.0: Version 3.0.30729.4926 SP 2 installed
v3.0/Setup: Version 3.0.30729.4926
v3.0/Setup/1031: Version 3.0.30729.4926 SP 2 installed
v3.0/Setup/Windows Communication Foundation: Version 3.0.4506.4926
v3.0/Setup/Windows Presentation Foundation: Version 3.0.6920.4902
v3.5: Version 3.5.30729.4926 SP 1 installed
v3.5/1031: Version 3.5.30729.4926 SP 1 installed
v4/Client: Version 4.0.30319 installed
v4/Client/1033: Version 4.0.30319 installed
v4/Full: Version 4.0.30319 installed
v4/Full/1033: Version 4.0.30319 installed

Generic command-line switches

All utilities use the same command-line parser, and therefore provide generic functionality for command-line switches:

Command-line switches can be prefixed by “-” or “/” (dash or slash), and switches are typically case-insensitive except if case-sensitive switches are defined.

The “-?” switch (alternatives /?, -h, /h) display the implemented switches and allowed or required parameters. If the command requires switches and/or parameters, the help information is displayed if no parameters are present.

The -in and -out switches set the stream encodings where applicable.

The -ci switch sets the culture info where applicable.

The first version of cmd.net is available for download here, and contains the commands described in this post.


Introducing cmd.net

April 11, 2011

Recently I had to analyze a couple of files for an import procedure using grep and find and sort and uniq and, wait, there is still no uniq command in Windows?

After giving up sketching a solution using cmd batch, I thought about writing a two-liner uniq command in C#.

Then, brainstorming set in, and a couple of command-line utilities came to mind that could be implemented in a command-line tool kit.

Of course, since this all would be implemented in a .Net language, I could make use of .Net concepts such as Encodings and CultureInfos. To support a broad range of Windows/.Net installations, the utilities are built for both .Net 3.5 and 4.0.

I am certainly aware of other utility collections such as UnxUtils, NirCmd, Cygwin, and MinGW, and therefore I will implement only commands that are useful, interesting to code, and/or might make use of additional .Net functionality.

The next couple of posts will deal with utilities in the cmd.net collection.

The first version of cmd.net is available for download here, and contains the commands listci, listenc, netenv, netver, datetime, df, enc, guid, tee, xslt, uniq.