How to Keep Some Lines from set of Lines Based on Condition?

Dears,

How to Keep 1st Line( Containing Work “Node”) , 2nd Line ( Containing Max Number of Characters) and Last one ( Containing “:”) from each set of Lines ? ( Highlighted here in BOLD Characters).

INPUT:

Node_01:$ → 1st Line
JANUARY 0 0 0 0 0 0 1 1 1 → 2nd Line
JANUARY 0 0 0 0 0
JANUARY 0 0 0 0 0 0
JANUARY 0 0
JANUARY 0 0 0 0 0 0
JANUARY 0:31 → 3rd Line
Node_02:$ -->1st Line
JANUARY 1 0 0 0 0 0 0 0 0 -->2nd Line
JANUARY 1 0 1 0 0
JANUARY 0 0 0 0 0 0
JANUARY 0 0
JANUARY 0 0 0 0 0 0
JANUARY 0:20 -->3rd Line
Node_03:$ -->1st Line
JANUARY 0 0 0 0 1 0 0 0 0 -->2nd Line
JANUARY 0 0 0 0 0
JANUARY 0 0 0 0 0 0
JANUARY 0 0
JANUARY 0 0 0 0 0 0
JANUARY 0:11 -->3rd Line

At the end I need to have something like that :

OUTPUT:

1st Line 2nd Line 3rd Line
Node_01 0 0 0 0 0 0 1 1 1 0:31
Node_02 1 0 0 0 0 0 0 0 0 0:20
Node_03 0 0 0 0 1 0 0 0 0 0:11
Node_N

Thanks in Advance

Hi,

Can you try the following sample?

mc = System.Text.RegularExpressions.Regex.Matches(strData,"(?<FirstLine>Node_.*?)\r?\n(?<SecondLine>.*?)\r?\n([^:]*?\r?\n)*(?<ThirdLine>.*?)(\r?\n|$)([^:]*?\r?\n)*(?=Node_|$)")

dtResult = mc.Cast(Of Match).Select(Function(m) dtResult.LoadDataRow({m.Groups("FirstLine").Value,m.Groups("SecondLine").Value,m.Groups("ThirdLine").Value},False)).CopyToDataTable()

Sample20220213-2.zip (3.0 KB)

Regards,

1 Like

Excellent @Yoichi , But how to make it more general ( when number of Nodes is changing), Possible?

Hi,

The above sample will work even if number of nodes which has same pattern is increased ( or decreased).

Regards,

1 Like

YES, Indeed :+1: Thanks a lot @Yoichi

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.