开发者

Compare 2 Text files line by line and replace when true or false

I have 2 text (ini) files to compare. first one reads

NUMM=1
DURC=360
VORS=2500
SPAN=130

the second one reads

NUMM=0
DURC=340
VORS=3000
SPAN=140

in a third 开发者_运维技巧file I say which data form the first file should be replace in the second file by telling true or false

NUMM=false
DURC=true
VORS=true
SPAN=false

Result of the second file after replaceing should be

NUMM=1
DURC=340
VORS=3000
SPAN=130

I tried a few ways but could not work it out in C#


What I would do :

First, read and split the true/false file, to create a HashTable named htTrueFalse.

Then, read and split the first file, to create a HashTable named htReplace.

Finally, read and split the second file, and for each key, get from the htTrueFalse if I need to replace, and if I need to, get the value for the key from the htReplace, and replace it in the result file.

Mmmh ... Im'not sure this is particularly clear, if not, don't hesitate to ask for an example.


You could do something like this.

var file1 = new StreamReader(filename1);
var file2 = new StreamReader(filename2);
var file3 = new StreamReader(filename3);
var output = string.Empty;
var line = string.Empty;

while((line = file1.ReadLine()) != null)
{
    var replace = file3.ReadLine.Split('=')[1];
    if(replace == "true")
    {
        output += file2.ReadLine() + "\r\n";
    }
    else
    {
        output += line + "\r\n";
    }
}

file1.Close;
file2.Close;
file3.Close;

var file4 = new StreamWriter(filename4);
file4.Write(output);
file4.Close;

Hope this helps.


Use LINQ to get the final output and then write it to file.

public class TempList
{
    public TempList(string key1, string data1)
    {
        key = key1;
        data = data1;
    }
    public TempList()
    { }

    public string key;
    public string data;
}
{
    System.Collections.Generic.List<TempList> list1 = new System.Collections.Generic.List<TempList>();
        list1.Add(new TempList("NUMM", "1"));
        list1.Add(new TempList("DURC", "360"));
        list1.Add(new TempList("VORC", "2500"));

        System.Collections.Generic.List<TempList> list2 = new System.Collections.Generic.List<TempList>();
        list2.Add(new TempList("NUMM", "0"));
        list2.Add(new TempList("DURC", "340"));
        list2.Add(new TempList("VORC", "3000"));

        System.Collections.Generic.List<TempList> list3 = new System.Collections.Generic.List<TempList>();
        list3.Add(new TempList("NUMM", "false"));
        list3.Add(new TempList("DURC", "true"));
        list3.Add(new TempList("VORC", "false"));

        System.Collections.Generic.List<TempList> p = (from q in list1
                join w in list2 on q.key equals w.key
                join r in list3 on q.key equals r.key
                select new TempList
                {
                    key = q.key,
                    data = (r.data == "true") ? w.data : q.data
                }).ToList<TempList>();
    }

Use Textreader to populate the list1, list2 and list3 from CSV files and TextWriter to write the final ouput p to a file.


This is what i would do:

System.Collections.Generic.List<string> fileToWrite = new List<string>();
string[] file1 = System.IO.File.ReadAllLines(filePath);
string[] file2 = System.IO.File.ReadAllLines(anotherFilePath);
string[] fileWithTrueFalse = System.IO.File.ReadAllLines(fileWithTrueFalsePath);
for (int i = 0; i < file1.Length; i++)
{
    if (fileWithTrueFalse[i].Substring(fileWithTrueFalse[i].IndexOf('=') + 1) == "true")
        fileToWrite.Add(file2[i]);
    else
        fileToWrite.Add(file1[i]);
}
System.IO.File.WriteAllLines(fileToWritePath, fileToWrite.ToArray());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜