finding the most recent build number in a huge text
hi i have a huge text file but i need to find the latest version(build number) and retrieve it to set it as a variable.
my text file:
...
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.001
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.002
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.003
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.004
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.005
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.006
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.006
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.006
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.007
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.008
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.009
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.010
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.011
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.012
sinsscm01:/mobile/开发者_JS百科6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.013
...
how should i go about finding the biggest build number? suppose that in this case 6.70.102.013 is the biggest build number.
so far what i did is:
string abc = "sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.001";
if (procOutput.Contains(abc))
{
// i am stuck here
}
where procOutput is currently StandardOutput.ReadToEnd(). I thus saved a textfile using Textwriter from StandardOutput.ReadToEnd()
You can do this using the following code snippet:
using (StreamReader reader = new StreamReader(path))
{
var text = reader.ReadToEnd();
var buildNo = text.SubString(text.Length - BuildNumberStringLength - 1, BuildNumberStringLength);
}
You can use this code:
string string greatestVersionNumber = ""
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
var line = sr.ReadLine());
var versionNumber = line.Replace(
@"sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_", "");
if(versionNumber.Length != line.Length)
greatestVersionNumber = versionNumber;
}
}
greatestVersionNumber
will contain "6.70.102.013", even if there are lines with unrelated text in between or after the line with the version numbers.
There is only one condition:
The line with the greates version number must be the last of the version number lines.
Read all the lines and extract each build number in turn. Convert it to an int by removing the periods and compare it to the current known highest. If higher, retain the build number.
using System.IO;
// ...
using(StreamReader rdr = new StreamReader(@"C:\myfile.txt"))
{
const string marker = "ANT_SASE_RELEASE_";
string maxBuildLabel = "";
int maxBuildNum = int.MinValue;
string line;
while((line = rdr.ReadLine()) != null)
{
int i = line.IndexOf(marker);
string buildLabel = line.Substring(i + marker.Length);
int buildNum = int.Parse(buildLabel.Replace(".", ""));
if (buildNum > maxBuildNum)
{
maxBuildLabel = buildLabel;
maxBuildNum = buildNum;
}
}
Console.WriteLine(maxBuildLabel);
}
I haven't tried compiling this code, but it should indicate one possible solution.
EDIT
Fixed a couple of syntax errors and verified that the algorithm worked (it did).
There is a complete code for you that i wrote now:It is based on Regular Expression and Lambda
using System.Text.RegularExpressions;
class Program
{
static IEnumerable<int> getBuildVersions(string BuildVersion)
{
string[] BuildVersionParts = BuildVersion.Split('.');
foreach (string part in BuildVersionParts)
{
yield return int.Parse(part);
}
}
static T[] HierarchicalMax<T>(IEnumerable<T[]> items)
{
var length = items.Min(v => v.Length);
IEnumerable<T[]> result = items;
for (int i = 0; i < length; i++)
{
int offset = i;
result = result.OrderByDescending(v => v[offset]);
}
T[] max = result.FirstOrDefault();
return max;
}
static void Main(string[] args)
{
string data = @"
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.001
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.002
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.003
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.004
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.005
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.006
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.006
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.006
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.007
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.008
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.009
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.010
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.011
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.012
sinsscm01:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.013
";
List<int[]> BuildVersions = new List<int[]>();
Regex rx = new Regex(@"\d+\.+\d+\.+\d+\d+\.+\d+");
foreach (Match m in rx.Matches(data))
{
BuildVersions.Add(getBuildVersions(m.Value).ToArray());
}
foreach (int[] version in BuildVersions)
{
Console.WriteLine(version[0].ToString() + " " + version[1].ToString() + " " + version[2].ToString() + " " + version[3].ToString());
}
int[] maxVersion = HierarchicalMax<int>(BuildVersions);
Console.WriteLine("MaxVersion is ");
Console.WriteLine(maxVersion[0].ToString() + " " + maxVersion[1].ToString() + " " + maxVersion[2].ToString() + " " + maxVersion[3].ToString());
Console.ReadLine();
}
}
精彩评论