从文本文件中获取过期的服务器名称 [英] Get outdated server name from a text file

查看:119
本文介绍了从文本文件中获取过期的服务器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从服务器列表中查找过期服务器。



将输入文件作为serverList.txt。数据中心有多台服务器。安装在多个服务器上的所有软件信息都保存在此文件中。



例如,保存在文件中的软件信息如下。



Server1,数据库,MySQL,5.5
Server2,数据库,MySQL,5.1

Server3,OS,Ubuntu,10.04

Server1,OS,Ubuntu,12.04

Server2,OS,Ubuntu,12.04

Server3,语言,Python,2.6.3



此文件表示Server1安装了MySQL版本5.5,Server2安装了版本5.1,Server3安装了版本10.04的Ubuntu。对于此程序,所有版本号的形式为XY或XYZ,其中X,Y和Z仅由数字组成。



编写一个读取此内容的程序文件,并打印至少有一个软件安装过时版本的服务器名称列表。



因此,在这种情况下,程序的输出应该是:



Server2

Server3



我试过的:



Find out of Date Servers from the list of the servers.

Take input file as serverList.txt. There are multiple servers in data center. All the software information installed on multiple servers is saved in this file.

For example, software information saved in the file is as follows.

Server1, Database, MySQL, 5.5
Server2, Database, MySQL, 5.1
Server3, OS, Ubuntu, 10.04
Server1, OS, Ubuntu, 12.04
Server2, OS, Ubuntu, 12.04
Server3, Language, Python, 2.6.3

This file indicates that Server1, has version 5.5 of MySQL installed, and Server2 has version 5.1 installed, and Server3 has version 10.04 of Ubuntu installed. For this program that all version numbers are of the form X.Y or X.Y.Z where X, Y, and Z are made up of only digits.

Write a program that reads this file, and prints a list of server names that have at least one software installation that is outdated version.

Thus, in this case, the output of your program should be:

Server2
Server3

What I have tried:

var list = new List<string>();
        //string strFileContent = string.Empty;
        using (StreamReader sr = new StreamReader(@"D:\input.txt"))
        {
            string Line;
            while ((Line = sr.ReadLine()) != null)
            {
                //strFileContent += Line + ",";
                list.Add(Line);
            }
        }





编辑(CHill60):OP进一步采用了代码...



EDIT (CHill60): OP has taken the code further...

引用:

已经尝试过,然后卡住了 -

have tried till here then stuck -

var list = new List<string>();
 //string strFileContent = string.Empty;


 Dictionary<string, string> latestVersion = new Dictionary<string, string>();
 using (StreamReader sr = new StreamReader(@"D:\input.txt"))
 {
     string Line;
     while ((Line = sr.ReadLine()) != null)
     {
         //strFileContent += Line + ",";
         list.Add(Line);
     }
 }
 string str = string.Empty;
 for (int i = 0; i <= list.Count; i++)
 {
     str = list[i].ToString();

     string[] stt = str.Split(',');

     for (int j = 0; j <= stt.Length; j++)
     {


     }
 }

推荐答案

家庭作业和练习由导师设定,以帮助您学习。他们给你机会申请和练习你正在教授的技巧。



所以,即使你真的问了一个问题,我们也不会做你的功课您。它根本不会帮助你。



提示:你发布的代码读取文件,但是你没有对行列表做任何事情也不是你将每行的内容与任何内容进行比较。



继续尝试,编写其余代码,如果卡住,请回来。







因为我建议你检查你将会发现本文有用的东西的价值:掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]



你已经接受了@ F-ES_Sitecore的建议并使用了 Split 获取每行的各个组件。如果你在
Homework and exercises are set by tutors to help you learn. They give you an opportunity to apply and practise the techniques you are being taught.

So, even if you had actually asked a question, we will not do your homework for you. It really would not help you at all.

Hints: The code you have posted reads the file, but you are not doing anything with the list of lines nor are you comparing the contents of each line to anything.

Keep trying, write the rest of the code and come back if you get stuck.



As I'm going to suggest that you examine values of things you are going to find this article useful: Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

You have taken the advice of @F-ES_Sitecore and used Split to get the individual components of each line. If you put a breakpoint on the line
for (int j = 0; j <= stt.Length; j++)

并检查你的数组 stt 你会注意到所有行

and examine your array stt you will notice that for all the lines

stt[0] contains the Server name - Server1, Server2, etc
stt[1] contains the "type" - Database, OS, Language
stt[2] contains the name of the actual thing of type stt[1] - MySQL, Ubuntu, Python
stt[3] contains the version number

因此你不需要使用 j 进行for循环,你可以直接看看您要检查的信息的确切位置。



您已经定义了字典 latestVersion 但是您需要填充它,可能使用添加方法 [< a href =https:// msd n.microsoft.com/en-us/library/bb338565(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]。现在只需硬编码值,直到你得到一些工作。



对于你想要查找的每一行 stt [ 2] 在你的字典中,并将 stt [3] 与字典中的值进行比较。如果它们不匹配,则需要将此服务器( stt [0] )添加到输出中。我建议使用 TryGetValue方法 [ ^ ]



给它一个去并看看你如何上场

So you don't really need that for-loop using j, you can just look directly at the exact location for the information you are going to check against.

You already have a Dictionary defined latestVersion but you need to populate that, probably using the Add method[^]. Just hard-code the values for now until you get something working.

For each line you are going to want to "lookup" stt[2] in your dictionary and compare stt[3] with the value in the dictionary. If they don't match then this server (stt[0]) needs to be added to your output. I suggest using the TryGetValue method[^]

Give that a go and see how you get on


注意:我完全同意 CHill60 [ ^ ],因为没有其他方法可以学习编程以开始编程。



几点提示:

1.阅读文本文件:< a href =https://msdn.microsoft.com/en-us/library/system.io.file.readalllines(v=vs.110).aspx> File.ReadAllLines Method(System.IO) [ ^ ]

Note: i completely agree with CHill60[^], because there's no other way to learn programming as to start programming.

Few tips:
1. Reading text file: File.ReadAllLines Method (System.IO)[^]
string[] lines = File.ReadAllLines("FullFileName.txt");



2.分割文字: String.Split方法(String [],StringSplitOptions)(系统) [ ^ ]

3.用于存储软件详细信息的自定义类:类(C#编程指南)| Microsoft Docs [ ^ ]

注意:类的构造函数必须接受逗号分隔的文本(一行)


2. Spliting text: String.Split Method (String[], StringSplitOptions) (System)[^]
3. Custom class to store software details: Classes (C# Programming Guide) | Microsoft Docs[^]
Note: a constructor of your class have to accept comma delimited text (one line)

public class SoftwareStorage
{
	private string sMachineName = string.Empty;
	private string sSoftwareType = string.Empty;
	private string sSoftwareName = string.Empty;
	private Version oSoftwateVersion = null;

	public SoftwareStorage(string delimitedText)
	{
		string[] parts = delimitedText.Split(new string[]{", "}, StringSplitOptions.RemoveEmptyEntries);
		sMachineName = parts[0];
		sSoftwareType = parts[1];
		sSoftwareName = parts[2];
		oSoftwateVersion = new Version(parts[3]); //see point 4.
	}
    //other members of class here
}



4。版本控制软件:版本类(系统) [ ^ ]

这个类提供了几种比较软件版本的方法。



祝你好运!


4. Versioning software: Version Class (System)[^]
This class provides several methods to compare version of software.

Good luck!


这篇关于从文本文件中获取过期的服务器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆