12233893487687687823 8736981111
52004554454450000411 8736981111,8736983268
62189347620012345611 8736981111,8736983268
799R0224082000000001 8736981111,8736983268
62789347620056665611 8736981111,8736983268
799R0324082000000001 8736981111,8736983268
62189347620012345611 8736981111,8736983268
799R0224082000000000 8736981111,8736983268
62189347620012345611 8736981111,8736983268
799R0224082000000000 8736981111,8736983268
82000000020089347620 8736981111,8736983268
52001021000020000407 8736981111,8736983269
62189347620099945458 8736981111,8736983269
799R0424082000000000 8736981111,8736983269
82000000020089347620 8736981111,8736983269
52001021000020000411 8736981111,8736983270
62189347620012345643 8736981111,8736983270
798C0224082000000000 8736981111,8736983270
82000000020089347620 8736981111,8736983270
52001021000020000407 8736981111,8736983271
62189347620099945458 8736981111,8736983271
798C0524082000000000 8736981111,8736983271
82000000020089347620 8736981111,8736983271
90000040000020000000 8736981111,8736983271
I have the file format as shown above, the starting line of the file starts with "1" which is the File header record, the line that start with "5" which is the batch header, the line that starts with "6" is the detail information in that batch record, line that starts with "7" is the additional batch detail. The lines "6" and "7" continues untill the line starts with "8" which indicates that one batch is finished. So the next batch again start with the line "5" indicating the start of the next batch. Similarly, the same steps will repeat as mentioned above and when the line starts with "9" it indicates that all the batch records are done for the file will and end this id indicates file footer information.
So, my question is - I want to add some unique identification number at the end of each line as shown in the above image (I have manually added those number after the space on end of each line) and I want a solution to read this text file and generate these unique numbers based on the above condition that I have explained in paragraph 1. So after the end of every batch i.e., line "8" it should start the next batch with the next unique number as shown in my image. Can you please suggest me how I can achieve this using Go language or C#.

Gowtham CpRank #1,552 · 1K reputationPosted Sep 2, 2024, 11:39 PM EDT
Hi ,
In C#, you can read the file line-by-line, identify the type of line based on its starting character, and append a unique batch number. Use a counter to track batches and increment it after encountering a line that starts with '8'. Here's the approach :
using System; using System.IO; class Program { static void Main() { string inputFile = "input.txt"; string outputFile = "output.txt"; int batchNumber = 1; using (StreamReader reader = new StreamReader(inputFile)) using (StreamWriter writer = new StreamWriter(outputFile)) { string line; while ((line = reader.ReadLine()) != null) { if ("6789".Contains(line[0])) line += $" 87369832{batchNumber:D2}"; writer.WriteLine(line); if (line.StartsWith("8")) batchNumber++; } } Console.WriteLine("File processing completed."); } }Similarly, in Go, read each line, check the starting character, and append a unique batch number. Use a scanner for reading and a writer for output. Here’s the approach:
package main import ( "bufio" "fmt" "os" ) func main() { inputFile, outputFile := "input.txt", "output.txt" file, _ := os.Open(inputFile) defer file.Close() outFile, _ := os.Create(outputFile) defer outFile.Close() scanner := bufio.NewScanner(file) writer := bufio.NewWriter(outFile) defer writer.Flush() batchNumber := 1 for scanner.Scan() { line := scanner.Text() if line[0] == '6' || line[0] == '7' || line[0] == '8' || line[0] == '9' { line += fmt.Sprintf(" 87369832%02d", batchNumber) } writer.WriteString(line + "\n") if line[0] == '8' { batchNumber++ } } fmt.Println("File processing completed.") }Kumar AURank #3,821 · 387 reputationPosted Sep 3, 2024, 5:19 PM EDT
Thank you so much for your time Gowtham , I liked your Go lang code , I will be using that code and it worked fine
Tahir AnsariRank #366 · 6.8K reputationPosted Sep 2, 2024, 10:57 PM EDT
Try this
using System; using System.IO; class Program { static void Main() { string inputFile = "input.txt"; string outputFile = "output.txt"; int uniqueNumber = 8736981111; using (StreamReader sr = new StreamReader(inputFile)) using (StreamWriter sw = new StreamWriter(outputFile)) { string line; while ((line = sr.ReadLine()) != null) { if (line.StartsWith("8")) { // Write the current line with the current unique number sw.WriteLine($"{line} {uniqueNumber}"); // Increment the unique number for the next batch uniqueNumber++; } else { // Write the current line with the current unique number sw.WriteLine($"{line} {uniqueNumber}"); } } } } }Kumar AURank #3,821 · 387 reputationPosted Sep 3, 2024, 5:22 PM EDT
Thank you Mohammad for your valuable time and I have tested your C# code, even that also worked perfectly, since my need was Go lang , so I will be using Go lang solution.
Thank you again for your help.