You could use PowerShell, which comes with Windows Vista or later:
$keys = @( );
Import-Csv input.txt | ForEach-Object {
if (!$_.head3) {
$keys | Out-File output.txt;
break;
} else {
if (!($keys -contains $_.head3)) {
$keys += $_.head3;
}
}
}
This may be slow on large amounts of data, since it is using an array ($keys) to hold and check unique keys. An alternative method is to write everything into a text file, sort it, and run it through Get-Unique. Another alternative is to use a hashtable (wouldn't help with memory usage, but would be faster than checking if something exists in an array).
This uses Import-Csv, which will take the first line as the headings. It then passes an array of objects (lines) to ForEach-Object. $_ is a variable referencing each object (line). .head3 is the property with the name head3, as defined in your example data as the column containing the keys. It checks if there is a value for this column in this line; if not it outputs to a file and quits as per your pseudocode. Note that non-key values may be accepted. If you have/need stricter rules for what is a key, you can check length, or do some RegEx pattern matching, etc..
a71,a72,a73,a74
a71,a72,keyC,a74
some message
a71,a72,keyD,a74
Currently, a73 counts as a key (it's in the third column, head3). The program will end at some message, since it does not have a third column, and will not read keyD.
If the line does have a key column, it checks if the key already exists in the array and, if not, adds it. Note that -contains is case insensitive. If this is a problem, it can be changed.
So you'll probably have to replace input.txt output.txt and head3 with the correct names. This was the simplest solution that does not modify the order of the data, though faster ones are possible if necessary.
Ctrl-K. – slhck♦ Oct 24 '12 at 9:11