在巨大的文本中查找最新的内部版本号

本文关键字:最新 内部 查找 版本号 巨大 文本 | 更新日期: 2023-09-27 17:56:13

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

我的文本文件:

...
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保存了一个文本文件

在巨大的文本中查找最新的内部版本号

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

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

您可以使用以下代码:

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 版本号的行必须是版本号行的最后一行。

读取所有行并依次提取每个内部版本号。通过删除句点将其转换为 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);
}

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

编辑

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

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