none
How to break line in a .txt file using C# after finding a paarticular text RRS feed

  • Question

  • I have a file abc.txt which contains data like below ..

    /* ----------------- test_cmd ----------------- */


    insert_job: test_cmd   job_type: CMD
    command: C\:\temp\test.exe
    machine: abc.com
    permission: gx,wx,mx
    date_conditions: 0
    timezone: EST5EDT
    application: MyApp

    /* ----------------- test1_cmd ----------------- */

    insert_job: test1_cmd   job_type: CMD
    command: C\:\temp\test1.exe
    machine: abc.com
    permission: gx,wx,mx
    date_conditions: 0
    timezone: EST5EDT
    application: MyApp

    I want to brek the line (which contains job_type) into 2 line and my output file (abc.txt) should contain data like below ..

    /* ----------------- test_cmd ----------------- */

    insert_job: test_cmd   
    job_type: CMD
    command: C\:\temp\test.exe
    machine: abc.com
    permission: gx,wx,mx
    date_conditions: 0
    timezone: EST5EDT
    application: MyApp

    /* ----------------- test1_cmd ----------------- */

    insert_job: test1_cmd   
    job_type: CMD
    command: C\:\temp\test1.exe
    machine: abc.com
    permission: gx,wx,mx
    date_conditions: 0
    timezone: EST5EDT
    application: MyApp


    Monday, March 11, 2019 12:08 PM

Answers

  • Hello,

    Try the following.

    var fileName1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test1_cmd.txt");
    var fileName2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test1_cmd1.txt");
    var newLines = new List<string>();
    var lines = File.ReadAllLines(fileName1);
    
    foreach (var line in lines)
    {
        if (line.Contains("job_type"))
        {
            var pos = line.IndexOf("job_type", StringComparison.Ordinal);
            var data1 = line.Substring(0, pos);
            var data2 = line.Substring(pos);
            newLines.Add(data1);
            newLines.Add(data2);
        }
        else
        {
            newLines.Add(line);
        }
    }
    
    File.WriteAllLines(fileName2, newLines.ToArray());


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 11, 2019 12:36 PM
    Moderator

All replies

  • Hello,

    Try the following.

    var fileName1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test1_cmd.txt");
    var fileName2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test1_cmd1.txt");
    var newLines = new List<string>();
    var lines = File.ReadAllLines(fileName1);
    
    foreach (var line in lines)
    {
        if (line.Contains("job_type"))
        {
            var pos = line.IndexOf("job_type", StringComparison.Ordinal);
            var data1 = line.Substring(0, pos);
            var data2 = line.Substring(pos);
            newLines.Add(data1);
            newLines.Add(data2);
        }
        else
        {
            newLines.Add(line);
        }
    }
    
    File.WriteAllLines(fileName2, newLines.ToArray());


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 11, 2019 12:36 PM
    Moderator
  • If you intend to try Regular Expressions too, then check this:

    string text = File.ReadAllText( "abc.txt" );

    string result = Regex.Replace( text, @"(?<=^.*\S[ ]+)(?=job_type:)", Environment.NewLine, RegexOptions.Multiline );

    File.WriteAllText( "abc.txt", result );


    Monday, March 11, 2019 5:16 PM
  • I have a file abc.txt which contains data like below ..

    /* ----------------- test_cmd ----------------- */


    insert_job: test_cmd   job_type: CMD
    command: C\:\temp\test.exe
    machine: abc.com
    permission: gx,wx,mx
    date_conditions: 0
    timezone: EST5EDT
    application: MyApp

    /* ----------------- test1_cmd ----------------- */

    insert_job: test1_cmd   job_type: CMD
    command: C\:\temp\test1.exe
    machine: abc.com
    permission: gx,wx,mx
    date_conditions: 0
    timezone: EST5EDT
    application: MyApp

    I want to brek the line (which contains job_type) into 2 line and my output file (abc.txt) should contain data like below ..

    /* ----------------- test_cmd ----------------- */

    insert_job: test_cmd   
    job_type: CMD
    command: C\:\temp\test.exe
    machine: abc.com
    permission: gx,wx,mx
    date_conditions: 0
    timezone: EST5EDT
    application: MyApp

    /* ----------------- test1_cmd ----------------- */

    insert_job: test1_cmd   
    job_type: CMD
    command: C\:\temp\test1.exe
    machine: abc.com
    permission: gx,wx,mx
    date_conditions: 0
    timezone: EST5EDT
    application: MyApp


    It'll be simpler using just the string's method Replace (after reading the file's content):

    string result=File.ReadAllText( "abc.txt" ).Replace("job_type:","\njob_type:");

    Monday, March 11, 2019 10:15 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.
    Best Regards,

    Jack

    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, March 13, 2019 5:07 AM
    Moderator