在一个巨大的文本中找到最新的内部版本号

时间:2011-05-10 09:44:21

标签: c#

嗨我有一个巨大的文本文件,但我需要找到最新版本(内部版本号)并检索它以将其设置为变量。

我的文字档案:

...
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
...

我应该如何寻找最大的内部版本号?假设在这种情况下6.70.102.013是最大的内部版本号。

到目前为止,我所做的是:

    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
    }

其中procOutput目前是StandardOutput.ReadToEnd()。因此,我使用StandardOutput.ReadToEnd()

中的Textwriter保存了一个文本文件

4 个答案:

答案 0 :(得分:0)

您可以使用此代码:

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将包含“6.70.102.013”,即使在包含版本号的行之间或之后有不相关文本的行。
只有一个条件:
带有greates版本号的行必须是版本号行的最后一行。

答案 1 :(得分:0)

您可以使用以下代码段执行此操作:

using (StreamReader reader = new StreamReader(path))
{
   var text = reader.ReadToEnd();
   var buildNo = text.SubString(text.Length - BuildNumberStringLength - 1, BuildNumberStringLength);
}

答案 2 :(得分:0)

阅读所有行并依次提取每个构建号。通过删除句点并将其与当前已知的最高值进行比较,将其转换为int。如果更高,请保留内部版本号。

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);
}

我没有尝试编译这段代码,但它应该表明一种可能的解决方案。

修改

修复了一些语法错误并验证算法是否有效(确实如此)。

答案 3 :(得分:0)

我现在写了一个完整的代码:它基于正则表达式和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();
        }
 }