hanzi_ru
hanzi_ru

Reputation: 153

Extract data from text file

I need to extract some data from a text file and insert to columns in excel sheet. I know how to do this if the rows and the length of the string is known.

try
{
    using (System.IO.StreamReader sr = new System.IO.StreamReader("test.txt")
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            listSNR.Items.Add(line.Substring (78,4));
        }
    }
}

But the particular text file is complex and the starting index or the length cannot be provided. But the starting word (PCPU01) of the row is known. Eg: PCPU01,T2716,0.00,0.01,0.00,0.00

output: T2716 0 0.01 0 0

In that case can somebody please let me know how to extract the texts?

Upvotes: 0

Views: 7868

Answers (5)

Uke
Uke

Reputation: 184

This was my approach in C#. It takes a string input (which you can get out of a text file) and an int with which line you want to get. It then separates the string at a given seperator char to a list which in turn is then read out. If the given line number is lower than the count of the created list, the entry is given back.

    public string GetLine(string multiline,int line)
    {
        List<string> lines = new List<string>();
        lines = multiline.Split('\n').ToList<string>();

        return lines.Count >= line ? lines[line] : "";
    }

Upvotes: 0

hanzi_ru
hanzi_ru

Reputation: 153

thanks for the answers. What i wanted is to identify the particular line in the text file and split the line into columns. So i was able to do this by calling a GetLine method:

string line15=GetLine(@"test.txt",15);
public string GetLine(string fileName, int line)
    {
        using (System.IO.StreamReader ssr = new System.IO.StreamReader("test.txt")) 
       //using (var ssr = new StreamReader("test.txt"))
        {
            for (int i = 1; i < line; i++)
                ssr.ReadLine();
            return ssr.ReadLine();
        }
    }

Then i splitted this line by using the delimiter (,)

Upvotes: 0

IS4
IS4

Reputation: 13217

using(System.IO.StreamReader sr = new System.IO.StreamReader("test.txt"))
{
    string line;
    while((line = sr.ReadLine()) != null)
    {
        string[] split = line.Split(',');
        //...
    }
}

split[0] will return "PCPU01", split[1] "T2716" and so on.

Upvotes: 2

John Willemse
John Willemse

Reputation: 6698

You can split one string into an array of strings, separated by a given character. This way, you could split the source string by a comma and use the resulting strings to build your output. Example:

string source = "PCPU01,T2716,0.00,0.01,0.00,0.00";
string[] parts = source.Split(',');
StringBuilder result = new StringBuilder();

result.Append(parts[1]); // The second element in the array, i.e. T2716
result.Append(" ");
result.Append(parts[2]); // 0.00
... // And so on...
return result.ToString() // return a string, not a StringBuilder

Upvotes: 1

Harish
Harish

Reputation: 1519

I hope this helps a little bit. You might have to tweak it to your needs. But this is a higher level code that gives you general idea of extracting data off a notepad.

DialogResult result = openFileDialog.ShowDialog();

            Collection<Info> _infoCollection = new Collection<Info>();
            Collection<string> listOfSubDomains = new Collection<string>();

            string[] row;
            string line;

            // READ THE FILE AND STORE IT IN INFO OBJECT AND STORE TAHT INFO OBJECT IN COLLECTION
            try
            {
                using (StreamReader reader = new StreamReader(openFileDialog.FileName))
                {
                    while((line = reader.ReadLine()) != null)
                    {
                        Info _info = new Info();
                        row = line.Split(' ');

                        _info.FirstName = row[0];
                        _info.LastName = row[1];
                        _info.Email = row[2];
                        _info.Id = Convert.ToInt32(row[3]);

                        _infoCollection.Add(_info);

                    }
                }

catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

Upvotes: 0

Related Questions