c# - Taking a file and splitting it into 2 groups -
so below have piece of code take data file , split 2 groups, , b.
string path = @"c:\users\povermyer\documents\visual studio 2013\projects\danproject\pnrs\pnrs.log"; string[] lines = system.io.file.readalllines(path); var count = file.readlines(path).count(); list<string> groupa = lines.take(7678).tolist(); list<string> groupb = lines.skip(7678).take(5292).tolist();
for clarification, first group takes first 7678 lines of code , places group while second group skips first 7678 lines , places rest of lines, 5292 lines, in group. problem this, if use future files, might not contain 7678 fist , 5292. know beginning of first group starts , ends a, , second group starts b , ends b. question is, how code above place file 2 groups depending on how start , end?
also, lines start , end not alone. example, beginning of
***********begin processing pnrs*********** , end ************end processing pnrs************`
and same group b. please help!
in case ever have break out more groups, may want consider storing groups in dictionary<string, list<string>>
, key group name , value list containing group data.
update
if i'm understanding scenario, have data looks like:
"***********begin processing pnrs*********** beginning 1 ************end processing pnrs************", "***********begin processing pnrs*********** beginning 2 ************end processing pnrs************", "***********begin processing b pnrs*********** , end 1 ************end processing b pnrs************", "***********begin processing b pnrs*********** , end 2 ************end processing b pnrs************", "***********begin processing ab pnrs*********** morning 1 ************end processing ab pnrs************", "***********begin processing ab pnrs*********** morning 2 ************end processing ab pnrs************"
you want have grouped like:
a: [0] beginning 1 [1] beginning 2 b: [0] , end 1 [1] , end 2 ab: [0] morning 1 [1] morning 2
this work best regular expressions
, still suggest storing in dictionary<string, list<string>>
new code
/// <summary> /// separates list of string data groups of data /// </summary> /// <param name="data">array of string data</param> /// <param name="groupnames">array of group names</param> /// <returns>dictionary of list of string data broken groups</returns> private static dictionary<string, list<string>> separategroups(string[] data, params string[] groupnames) { return groupnames.todictionary( groupname => groupname, groupname => data.select(d => { match m = regex.match(d, string.format("^\\*{{11,}}begin processing {0} pnrs\\*{{11,}}\\s(.*)\\s\\*{{11,}}end processing {0} pnrs\\*{{11,}}$", groupname)); return m.success ? m.groups[1].value : string.empty; }).where(s => !string.isnullorempty(s)).tolist() ); }
usage:
string[] groupnames = new[] { "a", "b" , "ab" }; string[] lines = new[] { "***********begin processing pnrs*********** beginning 1 ************end processing pnrs************", "***********begin processing pnrs*********** beginning 2 ************end processing pnrs************", "***********begin processing b pnrs*********** , end 1 ************end processing b pnrs************", "***********begin processing b pnrs*********** , end 2 ************end processing b pnrs************", "***********begin processing ab pnrs*********** morning 1 ************end processing ab pnrs************", "***********begin processing ab pnrs*********** morning 2 ************end processing ab pnrs************" }; int count = lines.length; dictionary<string, list<string>> groups = separategroups(lines, groupnames); foreach (string key in groups.keys) { console.writeline(key + ":"); foreach (string value in groups[key]) { console.writeline(value); } }
results:
a: beginning 1 beginning 2 b: , end 1 , end 2 ab: morning 1 morning 2
old code
/// <summary> /// separates list of string data groups of data /// </summary> /// <param name="data">array of string data</param> /// <param name="groupnames">array of group names</param> /// <returns>dictionary of list of string data broken groups</returns> private dictionary<string, list<string>> separategroups(string[] data, params string[] groupnames) { return groupnames.todictionary( groupname => groupname, groupname => data.where(ag => ag.startswith(groupname) && ag.endswith(groupname)).tolist() ); }
usage:
string[] groupnames = new[] { "a", "b", "ab" }; string[] lines = file.readalllines(filepath); int count = lines.length dictionary<string, list<string>> groups = separategroups(lines, groupnames);
Comments
Post a Comment