I am having output in the following manner. I am trying to parse that output and assign it to a datatable but i am facing difficulty because the output column text have spaces between the character because of that i am unable to split data in equal columns
sample o/p
List of Sessions
Cell Manager: prd.cp.sya.com
Creation Date: 10/11/2018 10:53:48 AM
Session Type Specification Status Mode Start Time Queuing Duration GB Written # Media # Errors # Warnings # Files Success Session ID
Backup IDB IDB_Backup Completed full 10/10/2018 11:50:07 0:00 1:07 1345.11 1 0 0 23794 100% 2018/10/10-38
Copy (scheduled) Daily_Incr_Copy Aborted - 10/10/2018 1:13:07 P 0:00 10:10 0.00 1 1 1 0 0% 2018/10/10-40
Backup EDRFT8 quESDapg-scan_DB_P8RDEUA_Da Completed full 10/10/2018 3:00:07 P 0:00 0:08 2.64 1 0 0 1 100% 2018/10/10-41
Backup ESRDE8 ptfrapg-scan_DB_P8WSEROD2_D Completed full 10/10/2018 3:00:07 P 0:00 0:10 3.35 1 0 0 1 100% 2018/10/10-42
Backup Backup_Servers_Weekly_Full_Daily_In Completed/Errors incr 10/10/2018 5:00:07 P 0:00 0:40 88.63 1 1 50 377910 100% 2018/10/10-43
Backup Prdesrda01_Daily backup Completed incr 10/10/2018 5:00:07 P 0:00 0:06 0.00 1 0 0 7214 100% 2018/10/10-44
Backup Win_FS_Daily_Incr_44 Completed incr 10/10/2018 5:45:07 P 0:00 2:42 305.12 1 0 0 1369158 100% 2018/10/10-45
Backup Win_FS_DMZ_Daily_Incr_01 Completed incr 10/10/2018 5:45:07 P 0:00 0:39 94.10 1 0 60 1158973 100% 2018/10/10-46
Backup PRRDFTFS01_Daily_Incr Completed incr 10/10/2018 6:00:07 P 0:00 0:15 14.81 1 0 72 128805 100% 2018/10/10-47
Backup PREWQAFS02_Daily_Incr Completed incr 10/10/2018 6:00:07 P 0:00 2:21 12.14 1 0 0 658882 100% 2018/10/10-48
Backup PRDTRFGS03_Daily_Incr Completed incr 10/10/2018 6:00:07 P 0:00 3:23 130.02 1 0 0 1172956 100% 2018/10/10-49
Backup Win_FS_Daily_Incr_23 Completed incr 10/10/2018 6:00:08 P 0:00 3:50 127.36 1 0 0 2343249 100% 2018/10/10-50
Backup Win_FS_Daily_Incr_21 Completed incr 10/10/2018 6:00:08 P 0:00 1:47 389.71 1 0 0 1966853 100% 2018/10/10-51
Backup Win_FS_Weekly_Full_01 Completed full 10/10/2018 6:30:07 P 0:00 2:58 1434.19 1 0 0 7311244 100% 2018/10/10-52
Backup Win_FS_Weekly_Full_30 Completed/Errors full 10/10/2018 6:30:08 P 0:00 3:14 1616.41 1 1 0 7037181 100% 2018/10/10-53
Backup Win_FS_Weekly_Full_32 Completed full 10/10/2018 6:30:08 P 0:00 4:07 1229.42 1 0 0 1195721 100% 2018/10/10-54
Backup Win_FS_Weekly_Full_33 Completed full 10/10/2018 6:30:08 P 0:00 2:13 629.20 1 0 0 6740762 100% 2018/10/10-55
Backup ORDTFC8 DeRDTapg53_test Backup_Arch Completed full 10/10/2018 6:51:07 P 0:00 0:07 8.00 1 0 0 1 100% 2018/10/10-56
Backup PRDRDFSW35_MSSQL_Offline_1AM_Daily_ Completed incr 10/10/2018 10:00:07 0:06 1:42 640.34 1 0 1 687 100% 2018/10/10-77
What I have tried:
I tried the following method:
string[] stringSeparators = new string[] {"\r\n"};
string[] lines = OutputFilePath.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
lines = lines.Where(x => !string.IsNullOrEmpty(x)).ToArray();
foreach (var item in lines.Skip(5))
{
formatedop = Regex.Split(item, @"\s{1,}");
formatedop = formatedop.Where(x => !string.IsNullOrEmpty(x)).ToArray();
try
{
if (formatedop.Length > 16)
{
if (formatedop[1].Contains("(scheduled)"))
{
formatedop[0] = formatedop[0] + " " + formatedop[1];
formatedop[5] = formatedop[5] + " " + formatedop[6] + " " + formatedop[7];
table.Rows.Add(
formatedop[0].ToString(),
formatedop[2].ToString(),
formatedop[3].ToString(),
formatedop[4].ToString(),
formatedop[5].ToString(),
formatedop[8].ToString(),
formatedop[9].ToString(),
formatedop[10].ToString(),
formatedop[11].ToString(),
formatedop[12].ToString(),
formatedop[13].ToString(),
formatedop[14].ToString(),
formatedop[15].ToString(),
DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
}
if (formatedop[1].Contains("Oracle8"))
{
formatedop[1] = formatedop[1] + " " + formatedop[2];
formatedop[5] = formatedop[5] + " " + formatedop[6] + " " + formatedop[7];
table.Rows.Add(
formatedop[0].ToString(),
formatedop[1].ToString(),
formatedop[3].ToString(),
formatedop[4].ToString(),
formatedop[5].ToString(),
formatedop[8].ToString(),
formatedop[9].ToString(),
formatedop[10].ToString(),
formatedop[11].ToString(),
formatedop[12].ToString(),
formatedop[13].ToString(),
formatedop[14].ToString(),
formatedop[15].ToString(),
DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
}
}
else
{
formatedop[5] = formatedop[5] + " " + formatedop[6] + " " + formatedop[7];
table.Rows.Add(
formatedop[0].ToString(),
formatedop[1].ToString(),
formatedop[2].ToString(),
formatedop[3].ToString(),
formatedop[4].ToString(),
formatedop[5].ToString(),
formatedop[8].ToString(),
formatedop[9].ToString(),
formatedop[10].ToString(),
formatedop[11].ToString(),
formatedop[12].ToString(),
formatedop[13].ToString(),
formatedop[14].ToString(),
formatedop[15].ToString(),
DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
}
}
catch(System.Exception ex)
{
formatedop[0].ToString();
Console.WriteLine(ex.InnerException.ToString());
}
}
But this is not working properly. It breaks at one or the other line. I need a clean and clear method. Can anyone suggest something. I need output like this:
formatedop[0]=Backup
formatedop[1]=IDB IDB_Backup
formatedop[2]=Completed
formatedop[3]=full
formatedop[4]=10/10/2018 11:50:07
formatedop[5]=0:00
formatedop[6]=1:07
formatedop[7]=1345.11
formatedop[8]=1
formatedop[9]=0
formatedop[10]=0
formatedop[11]=23794
formatedop[12]=100%
formatedop[13]=2018/10/10-38