writing into csv file

Posted by arin on Stack Overflow See other posts from Stack Overflow or by arin
Published on 2012-04-05T23:25:49Z Indexed on 2012/04/05 23:29 UTC
Read the original article Hit count: 296

Filed under:
|
|

I recived alarms in a form of text as the following

NE=JKHGDUWJ3  Alarm=Data Center STALZ AC Failure  Occurrence=2012/4/3 22:18:19 GMT+02:00  Clearance=Details=Cabinet No.=0, Subrack No.=40, Slot No.=0, Port No.=5, Board Type=EDS, Site No.=49, Site Type=HSJYG500 GSM, Site Name=Google1  .

I need to fill it in csv file so later I can perform some analysis

I came up with this

namespace AlarmSaver
{
    public partial class Form1 : Form
    {
        string filesName = "c:\\alarms.csv";
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (!File.Exists(filesName))
            {
                string header = "NE,Alarm,Occurrence,Clearance,Details";
                File.AppendAllText(filesName,header+Environment.NewLine);
            }
            StringBuilder sb = new StringBuilder();
            string line = textBoxAlarm.Text;

            int index = line.IndexOf("  ");
            while (index > 0)
            {
                string token = line.Substring(0, index).Trim();
                line = line.Remove(0, index + 2);

                string[] values = token.Split('=');
                if (values.Length ==2)
                {
                    sb.Append(values[1] + ",");
                }
                else
                {
                    if (values.Length % 2 == 0)
                    {
                        string v = token.Remove(0, values[0].Length + 1).Replace(',', ';');
                        sb.Append(v + ",");
                    }
                    else
                    {
                        sb.Append("********" + ",");
                        string v = token.Remove(0, values[0].Length + 1 + values[1].Length + 1).Replace(',', ';');
                        sb.Append(v + ",");
                    }
                }
                index = line.IndexOf("  ");
            }
            File.AppendAllText(filesName, sb.ToString() + Environment.NewLine);
        }
    }
}

the results are as I want except when I reach the part of

Details=Cabinet No.=0, Subrack No.=40, Slot No.=0,
Port No.=5, Board Type=KDL, Site No.=49, Site Type=JDKJH99 GSM, Site Name=Google1 .

I couldnt split them into seperate fields.

as the results showing is as

Example results

I want to split the details I want each element in details to be in a column

to be somthin like

enter image description here

its realy annoying :-)

please help , thank you in advance

© Stack Overflow or respective owner

Related posts about c#

Related posts about text