Click here to Skip to main content
15,907,236 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi,

Please help me, i am very new to c#,

I get 5 txt files daily with more then 1500 entries in each txt file, i need to search the each line for specific strings and copy the entire line to a new file.

Entries look as show
5002290035007540002907201900000075000005378644600029100100000000000

I need to search the string 537864460 if exist paste the entire line into new file

after copying, need to rearrange the line

as shown below

53786446050022900350075400029072019000000750000000029100100000000000

What I have tried:

i was trying with BAT file, but couldn't achieve it
Posted
Updated 22-Aug-19 0:11am
Comments
BillWoodruff 20-Aug-19 21:27pm    
If you add some information about usage, you may get additional information.

1. can there be more than one line in a given file that contains a match ?

2. do you work with one file at a time ?

3. will the match always occur at the same position in the line ?

4. are there any other consistent areas of content in one given file, or all files ?
Member 2731148 21-Aug-19 0:29am    
1: yes there will more then one Line match with given string in each file
2) Yes, one txt File at a time
3) Yes, The string will start from 40th column Position in each line
4) Only consistent content for the Lines in all the files are Zeros at the end of each line
BillWoodruff 22-Aug-19 4:44am    
Good information ! I will respond with some ideas today (GMT + 7).

Other questions: is each line always the same length, and is the match string always the same length.
Member 2731148 22-Aug-19 5:03am    
yes the length of line and match string are always constant

Since you talk about C#, lets stick with a dev solution, and hope yoiu coding skills are up to it.
Reading the whole file into memory is simple:
C#
string[] lines = File.ReadAllLines(@"D:\Test Data\MyFile.txt");
That reads the whole file in, one line per element of the array. You can then use a foreach loop to process each line at a time:
int start = line.IndexOf("537864460");
if (start >= 0)
   {
   // Line contains the text, start says where it starts.
   ...
   }
You can then use String.Substring to extract the parts of the string you want, reorder them into a new string, and write that new string to a file.

It's not complex - but that depends on your knowledge and skill level, and only you can judge that. :laugh:
 
Share this answer
 
Comments
CPallini 21-Aug-19 2:46am    
5.
Maciej Los 21-Aug-19 5:41am    
5ed!
The following example tries to minimize the creation of new strings by using StringBuilder: Strings in C# are immutable; every operation on a String creates a new String; a StringBuilder is mutable: operations like 'Insert, 'Append, 'Remove, modify the internal, data, and return a reference to the StringBuilder itself.

The example shown here may be "excessive:" ... in the sense that using a StringBuilder to fabricate each line may not be saving that much memory:
public class FindMatchInFile
{
    private StringBuilder filesb; 
    private StringBuilder linesb; 

    private string line;
    private int match, lineschanged;

    public FindMatchInFile(string readfilePath, string savefilepath, string matchstring)
    {
        filesb = new StringBuilder();
        linesb = new StringBuilder();

        using (StreamReader reader = new StreamReader(readfilePath))
        {
            while (reader.Peek() >= 0)
            {
                line = reader.ReadLine();
                
                match = line.IndexOf(matchstring);

                if (match == -1)
                {
                    filesb.AppendLine(line);
                }
                else
                {
                    lineschanged++;

                    linesb.Clear();

                    linesb.Append(line);

                    linesb.Remove(match, matchstring.Length);

                    linesb.Insert(0, matchstring);
                    
                    filesb.AppendLine(linesb.ToString());
                }
            }

            reader.Close();
        }

        if(lineschanged > 0)
        {
            using (StreamWriter writer = new StreamWriter(savefilepath))
            {
                writer.Write(filesb.ToString());
                writer.Close();
            }
        }
        else
        {
            MessageBox.Show("No matches found");
        }
    }
}
 
Share this answer
 
Comments
Maciej Los 22-Aug-19 6:22am    
:thumbsup:
BillWoodruff 22-Aug-19 7:39am    
thanks, Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900