writing into csv file

Go To StackoverFlow.com

2

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

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

2012-04-05 23:25
by arin
split when commas I tried to count the = , and now Im trying REGEX but Im making things worse , I really cant figuer it out :- - arin 2012-04-05 23:37


1

Split using this regex expression

string[] values = Regex.Split(line,
                              @",?\s*\w+(\sNo\.|\sType|\sName)?=",
                              RegexOptions.ExplicitCapture);

It should split the whole line at once. The only thing it cannot do, is to remove the "  ." at the end of the line and it will also yield some unwanted entries that you will have to remove (I marked them with a minus sign). This is what I get as result:

    [0]-: ""  
    [1] : "JKHGDUWJ3"  
    [2] : "Data Center STALZ AC Failure"  
    [3] : "2012/4/3 22:18:19 GMT+02:00"  
    [4] : ""  
    [5]-: ""  
    [6] : "0"  
    [7] : "40"  
    [8] : "0"  
    [9] : "5"  
    [10] : "EDS"  
    [11] : "49"  
    [12] : "HSJYG500 GSM"  
    [13] : "Google1  ."  

--

Let me explain the Regex expression ,?\s*\w+(\sNo\.|\sType|\sName)?=

,?                                             an optional comma
\s*\w+                                     a word possibly preceded by spaces
(\sNo\.|\sType|\sName)?   optionally a " No." or a " Type" or a " Name"
=                                               an equal sign


UPDATE

The full code looks like this

if (!File.Exists(filesName)) {
    string header = "NE,Alarm,Occurrence,Clearance,CabinetNo,SubrackNo,SlotNo,PortNo,BoardType,SiteNo,SiteType,SiteName";
    File.AppendAllText(filesName, header + Environment.NewLine);
}
string line = textBoxAlarm.Text.TrimEnd(' ', '.');
string[] values = Regex.Split(line, @",?\s*\w+(\sNo\.|\sType|\sName)?=",
                              RegexOptions.ExplicitCapture);
List<string> valuesList = values.ToList();
valuesList.RemoveAt(5); // Remove the empty Details part.
valuesList.RemoveAt(0); // Remove the first empty part.
values = valuesList
    .Select(s => s == "" ? "********" : s)
    .ToArray();
File.AppendAllText(filesName, String.Join(",", values) + Environment.NewLine);
2012-04-05 23:58
by Olivier Jacot-Descombes
I added the parameter RegexOptions.ExplicitCapture. Regex includes matches parts in the parentheses (captues) in the result otherwise - Olivier Jacot-Descombes 2012-04-06 00:55
for some reasons I couldnt add this to my code can you please apply your technique on the code above I keep destroying the code :-( thank you in advance I liked your reply - arin 2012-04-07 19:31


2

After this line:

string v = token.Remove(0, values[0].Length + 1 + values[1].Length + 1).Replace(',', ';')

Do:

var detailParts = v.Split(";".ToCharArray());

Now fill the detail parts. and append them to sb, for filling detail parts you need do something like this.

detailPart[i].Split("=".ToCharArrray())[1]
2012-04-05 23:37
by Saeed Amiri
Saed can you post the full code or answer or more details its not showing any result - arin 2012-04-05 23:51
Ads