Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,
i want to merge two text file.but i failed to create logic.
my files are like
abc.txt:-
1|SD2|12345
2|SD2|23455
3|SD2|34564
4|SD3|12344
5|SD3|2345

second file
def.txt:-
1|SD2|5676
2|SD3|678978

Now i want to create a file on the basis of SD2 and SD3 field.
my final text would be like :-
final.txt
1|SD2|12345
2|SD2|23455
3|SD2|34564
4|SD2|5676
5|SD3|12344
6|SD3|2345
7|SD3|678978

if you get any idea then please response.
Thanks
Posted
Comments
LanFanNinja 3-Feb-12 3:01am    
Check my solution below (solution 4) hope it helps and let me know if it works for you.

This or some variation of this should work for you.

C#
List<string[]> data = new List<string[]>();
data.Add(File.ReadAllLines("abc.txt"));
data.Add(File.ReadAllLines("def.txt"));
List<string> finalData = new List<string>();

for (int i = 0; i < data.Count; i++)
{
    for (int n = 0; n < data[i].Length; n++)
    {
        string[] temp = data[i][n].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        finalData.Add(temp[1] + "|" + temp[2]);
    }
}

finalData.Sort();

using (StreamWriter writer = new StreamWriter("final.txt"))
{
    for (int i = 0; i < finalData.Count; i++)
    {
        writer.WriteLine((i + 1).ToString() + "|" + finalData[i]);
    }

    writer.Close();
}
 
Share this answer
 
Comments
Rajeev Jayaram 3-Feb-12 3:17am    
5up!
LanFanNinja 3-Feb-12 3:19am    
Thank you
Wendelius 3-Feb-12 17:08pm    
Very nice, 5
LanFanNinja 3-Feb-12 17:24pm    
Thank you Mika
Espen Harlinn 5-Feb-12 15:12pm    
5'ed!
Some alternative solution employing some IEnumerable<strin> extension methods plus some LINQ syntax:


  • Concat(), Aggregate()
  • from ... in ... let d = ... orderby d ascending select d


C#
public static void MergeFiles(string in1, string in2, string merge)
{
    var q = from t in File.ReadAllLines(in1).Concat(File.ReadAllLines(in2))
            let d = t.Substring(t.IndexOf('|')+1) orderby d ascending
            select d;
    using (StreamWriter w = new StreamWriter(merge))
        q.Aggregate(0, (a, d) => { w.WriteLine("{0}|{1}", ++a, d); return a; });
}



  1. Create a query that returns all truncated lines of both files (stripped off initial "...|"), sorted.
  2. process all lines and write to the stream, by counting the line number (the aggregation value).


Cheers

Andi
 
Share this answer
 
Comments
BillWoodruff 4-Feb-12 20:01pm    
+5 Great Linq example.
Andreas Gieriet 4-Feb-12 20:03pm    
Thanks for your 5!
LanFanNinja 5-Feb-12 6:42am    
+5 Very nice indeed!!
Andreas Gieriet 5-Feb-12 8:09am    
Thanks for your 5!
Merge Two Files with C#
and more results on
Google
 
Share this answer
 
Comments
BillWoodruff 4-Feb-12 20:03pm    
There is more to this question than just concatenating two text files: they must be merged by sorting.

best, Bill
You mean lines contains "SD2" word will come first in created file?

If it is a case, you can do it but you will have to apply the logic for doing the same...

Step 1: You can read all the lines which contains SD2 from first file and write on new file.
Step 2: Read all the lines which contains SD2 from second file and write on new file.
Step 3: and so on...

Kindly update if you have any doubt...
 
Share this answer
 
v2
a single line in batch wud do it
open a text file
and copy the below line
copy textfilename.txt + textfilename.txt newfile.txt

if there r many textfiles then copy all text files to a folder.
create a .bat file in the same folder and copy the line below
copy /b *.txt newfile.txt

save it with .bat extension
double click it to execute it

i prefer this because this wud b fast
 
Share this answer
 
Comments
Andreas Gieriet 4-Feb-12 17:54pm    
Well, the OP asks also to re-number the final file (see example text), and the entries should be grouped...

If you want to use shell, you might consider to use powershell, e.g.:

PS> $n = 0; cat *.txt | %{ $_ -replace '^\d+\|','' } | sort | %{ $n++; echo "$n|$_" } > res.txt

Andi

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