none
Increment the value for each new line for a text file RRS feed

  • Question

  • I have a text file which contains the below content:

    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01

    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02

    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01

    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09

    I want to convert above file content as below :

    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01

    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02

    UNH+03+ABC:1:91'BGM+::ABC++27'UNT+2+03

    UNH+04+ABC:1:91'BGM+::ABC++27'UNT+2+04

    it means value after UNH+ should be incremented by 1 for each new line and the same value we need to

    put after UNT+2 in each line.And these values should be unique. How can we achieve this via c# code ?


    Thanks in advance !

    Tuesday, December 11, 2018 8:22 AM

Answers

  •  Find below code which uses string operations.

                string[] data = File.ReadAllLines("D://Sample.txt");
                StreamWriter sw = new StreamWriter("D://Out.txt");
                int cnt = 0;
                foreach (string str in data)
                {
                    string[] outData = str.Split('+');
                    cnt++;
                    string numberOut = cnt.ToString("0#");
                    outData[1] = numberOut;
                    outData[outData.Length - 1] = numberOut;
                    string mergedOut = string.Join('+', outData);
                    sw.WriteLine(mergedOut);
                }
                sw.Close();

    Sample input file content

    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01

    Out file content

    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02
    UNH+03+ABC:1:91'BGM+::ABC++27'UNT+2+03
    UNH+04+ABC:1:91'BGM+::ABC++27'UNT+2+04
    UNH+05+ABC:1:91'BGM+::ABC++27'UNT+2+05
    UNH+06+ABC:1:91'BGM+::ABC++27'UNT+2+06
    UNH+07+ABC:1:91'BGM+::ABC++27'UNT+2+07
    UNH+08+ABC:1:91'BGM+::ABC++27'UNT+2+08
    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09
    UNH+10+ABC:1:91'BGM+::ABC++27'UNT+2+10
    UNH+11+ABC:1:91'BGM+::ABC++27'UNT+2+11
    UNH+12+ABC:1:91'BGM+::ABC++27'UNT+2+12
    UNH+13+ABC:1:91'BGM+::ABC++27'UNT+2+13

    • Marked as answer by Shivay_ Monday, December 17, 2018 10:43 AM
    Tuesday, December 11, 2018 2:34 PM

All replies

  • I didn't understand well, but this gives a similar output

                //UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
                string textFileLocation = @"W:\GetM\iamtext.txt";
                List<string> contents1 = new List<string>();
                for (int i = 0; i < 5; i++)
                {
                    contents1.Add($"UNH+{i}+ABC:1:91'BGM+::ABC++27'UNT+" +
                    $"{i}+01");
                }
                try
                {
                        File.AppendAllLines(textFileLocation, contents1);
                    Console.WriteLine("Success");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }



    BP-LP 2005/2016 @ll rights reserved


    Tuesday, December 11, 2018 9:18 AM
  •  Find below code which uses string operations.

                string[] data = File.ReadAllLines("D://Sample.txt");
                StreamWriter sw = new StreamWriter("D://Out.txt");
                int cnt = 0;
                foreach (string str in data)
                {
                    string[] outData = str.Split('+');
                    cnt++;
                    string numberOut = cnt.ToString("0#");
                    outData[1] = numberOut;
                    outData[outData.Length - 1] = numberOut;
                    string mergedOut = string.Join('+', outData);
                    sw.WriteLine(mergedOut);
                }
                sw.Close();

    Sample input file content

    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09
    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01

    Out file content

    UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01
    UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02
    UNH+03+ABC:1:91'BGM+::ABC++27'UNT+2+03
    UNH+04+ABC:1:91'BGM+::ABC++27'UNT+2+04
    UNH+05+ABC:1:91'BGM+::ABC++27'UNT+2+05
    UNH+06+ABC:1:91'BGM+::ABC++27'UNT+2+06
    UNH+07+ABC:1:91'BGM+::ABC++27'UNT+2+07
    UNH+08+ABC:1:91'BGM+::ABC++27'UNT+2+08
    UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09
    UNH+10+ABC:1:91'BGM+::ABC++27'UNT+2+10
    UNH+11+ABC:1:91'BGM+::ABC++27'UNT+2+11
    UNH+12+ABC:1:91'BGM+::ABC++27'UNT+2+12
    UNH+13+ABC:1:91'BGM+::ABC++27'UNT+2+13

    • Marked as answer by Shivay_ Monday, December 17, 2018 10:43 AM
    Tuesday, December 11, 2018 2:34 PM
  • Hi Shivay_,

    Thank you for posting here.

    For your question, you want to increment the value for each new line for a text file.

    Please try this code below.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    
    namespace test1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string txtPath = @"C:\Users\Administrator\Desktop\2.txt";
                StreamReader sd = new StreamReader(txtPath);
                string m = sd.ReadToEnd();
                sd.Close();
                FileStream stream = File.Open(txtPath, FileMode.OpenOrCreate, FileAccess.Write);
                stream.SetLength(0);
                stream.Close();
                StreamWriter sw = new StreamWriter(txtPath);
                string[] m1 = m.Split('\r');
                for (int i = 0; i < m1.Length; i++)
                {
                    string m2 = m1[i].Remove(m1[i].IndexOf('0')+1 , 1).Remove(m1[i].LastIndexOf('0'), 1).Insert(m1[i].IndexOf('0') + 1, (i + 1).ToString()).Insert(m1[i].LastIndexOf('0')+1 , (i + 1).ToString());
                    Console.WriteLine(m2);
                    m1[i] = m1[i].Replace(m1[i], m2);
                    sw.WriteLine(m1[i]);
                }
                sw.Flush();
                sw.Close();
                Console.WriteLine("success");
                Console.ReadKey();
            }
        }
    }
    

    Before:

    After:


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 12, 2018 7:30 AM
    Moderator
  • if you want to regular expression to do that, you can do as below.

     string input = @"UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01                        UNH+02+ABC:1:91'BGM+::ABC++27'UNT+2+02                        UNH+01+ABC:1:91'BGM+::ABC++27'UNT+2+01                        UNH+09+ABC:1:91'BGM+::ABC++27'UNT+2+09";
    
     Regex rx = new Regex("(UNH.{3})");      
     int i = 1;
     string result = rx.Replace(input, match => { return match.Value.Substring(0,4) + i++.ToString().PadLeft(2,'0'); });
                
     rx = new Regex("(UNT.{5})");      
     i = 1;
     result = rx.Replace(result, match => { return match.Value.Substring(0,6) + i++.ToString().PadLeft(2,'0'); });
                           
    Console.WriteLine(result);

    Wednesday, December 12, 2018 4:37 PM