-1
E2739158012008-10-01O9918107NPF7547379999010012008-10-0100125000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
E2739158PU0000-00-00                     010012008-10-0100081625219                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
E3180826011985-01-14L9918007NPM4927359999010011985-01-1400005620000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
E3180826PU0000-00-00                     020011985-01-14000110443500021997-01-1400000518799                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
E3292015011985-01-16L9918007NPM4927349999010011985-01-1600003623300   

I have this flat file and I need to group this based on the 2nd position to 8th position example(2739158/3180826/3292015) and write to another flat file. So the data Starting with 'E' should Repeat in the single line along with that group field(2nd to 8th Position in the start) and I should take the 9th Position after 'E'

Also I need to replace Empty space with ('*' star) For example

1st Line

2739158**E**012008-10-01O9918107NPF7547379999010012008-10-0100125000000*****E**012008-10-01O9918107NPF7547379999010012008-10-0100125000000  

2nd Line

3180826**E**011985-01-14L9918007NPM4927359999010011985-01-1400005620000**E**011985-01-14L9918007NPM4927359999010011985-01-140000562000**E**011985-01-14L9918007NPM4927359999010011985-01-140000562000***

3rd Line

3292015**E**011985-01-16L9918007NPM4927349999010011985-01-1600003623300****   

Can we do this in Stream reader c#, please? Any help would be highly appreciated.The file size is more than 285 MB so it it good to read through Stream Reader? Thanks

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • Also, how do we know what your positions are if you don't tell us? And what do you mean you need to "group" based on the positions? Do you actually mean "sort"? – robbpriestley Dec 05 '19 at 16:03
  • @Selvin: Sorry, I tried using Stream reader to read the file and i was able to write but could couldn't group or add * for the empty space.I keep you posted and will send the error details later – sivaraman vasu Dec 05 '19 at 17:12
  • @robbpriestley: Position is starting from '1'.Here I have to group from position 2 to 8(7) characters.The file is already sorted.I just need to group – sivaraman vasu Dec 05 '19 at 17:14

2 Answers2

0

@jdweng: thanks very much for your input. i tried somehow without grouping and it works as expected.Thanks everyone who tried to solve the issue.

                                                                                                        string sTest= string.Empty;                                                                                                            List<SortLines> lines = new List<SortLines>();                
            List<String> FinalLines = new List<String>();
            using (StreamReader sr = new StreamReader(@"C:\data\Input1.txt))
            {                  
                    sr.ReadLine();

                string line = "";
                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();
                    //line = line.Trim();
                    if (line.Length > 0)
                    {
                        line = line.Replace(" ", "*");

                        SortLines newLine = new SortLines()
                        {
                            key = line.Substring(1, 7),
                            line = line
                        };                          
                        if (sTest != newLine.key)
                        {
                            //Add the Line Items to String List
                            sOuterLine = sTest + sOneLine;
                            FinalLines.Add(sOuterLine);
                            string sFinalLine = newLine.line.Remove(1, 7);
                            string snewLine = newLine.key + sFinalLine;
                            sTest = snewLine.Substring(0, 7);
                            //To hold the data for the 1st occurence
                            sOtherLine = snewLine.Remove(0, 7);
                            bOtherLine = true;
                            string sKey = newLine.key;
                            lines.Add(newLine);

                        }
                        else if (sTest == newLine.key)
                        {

                            string sConcatLine = String.Empty;
                            string sFinalLine = newLine.line.Remove(1, 7);
                            //Check if 1st Set
                            if (bOtherLine == true)
                            {
                                sOneLine = sOtherLine + sFinalLine;
                                bOtherLine = false;
                            }
                        //If not add subsequent data
                            else
                            {
                                sOneLine = sOneLine + sFinalLine;
                            }
                            //Check for the last line in the flat file
                            if (sr.Peek() == -1)
                            {
                                sOuterLine = sTest + sOneLine;
                                FinalLines.Add(sOuterLine);
                            }                                                             
                        }

                    }

                }
            }
            //Remove the Empty List
            FinalLines.RemoveAll(x => x == "");              
            StreamWriter srWriter = new StreamWriter(@"C:\data\test.txt);

            foreach (var group in FinalLines)
            {
                srWriter.WriteLine(group);                    
            }
            srWriter.Flush();
            srWriter.Close();              
-1

Try code below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
         const string INPUT_FILENAME = @"c:\temp\test.txt";
         const string OUTPUT_FILENAME = @"c:\temp\test1.txt";
         static void Main(string[] args)
         {
             List<SortLines> lines = new List<SortLines>();
             StreamReader reader = new StreamReader(INPUT_FILENAME);
             string line = "";
             while ((line = reader.ReadLine()) != null)
             {
                 line = line.Trim();
                 if (line.Length > 0)
                 {
                     line = line.Replace(" ", "*");

                     SortLines newLine = new SortLines() { key = line.Substring(2, 7), line = line };
                     lines.Add(newLine);
                 }
             }
             reader.Close();
             var groups = lines.GroupBy(x => x.key);
             StreamWriter writer = new StreamWriter(OUTPUT_FILENAME);

             foreach (var group in groups)
             {
                 foreach (SortLines sortLine in group)
                 {
                     writer.WriteLine(sortLine.line);
                 }
             }
             writer.Flush();
             writer.Close();

         }
    }
    public class SortLines : IComparable<SortLines>
    {
        public string line { get; set; }
        public string key { get; set; }
        public int CompareTo(SortLines other)
        {
            return key.CompareTo(other);
        }
    }



}
jdweng
  • 33,250
  • 2
  • 15
  • 20