Hi ,
I have two text files.
madavas1.txt and madax3.txt.
There are different values in the files. I want a file which gives the difference between the commen value based on the fields below Gateway( please refer to the txt file attached)

new txt  = madavas1.txt - madax3.txt.

for Eg: if the gateway value is 192.168.10.254 , this is there in both files. So i need to delete the entries in the same line where the gateway values are same and create the resulted text file.
Some logical idea will be helpful for me

These two might be overkill for what you are seeking (but they are in C#):

http://www.menees.com/index.html

http://www.codeproject.com/KB/recipes/diffengine.aspx

Do a net search on "Diff" which is the utility in *nix to do this kind of thing. I think (it's been eons since I've used it in any form) that it makes a table of all the strings in the file and compares each new entry to those in the table.

You can probably get away with using a list or a dictionary after parsing your text file and just using the Contains() method.

thanks for the link. Those looks more complex info than what i need. I will try to do some logic and will post the code here. That will be a better starting point than just the plain text

I once did something similiar to this in C++. Here's what I'd suggest you to do, considering that the files are going to be in the similar format/template as those provided.

Try to read the files, one line at a time. Since you do not need to read the first two lines, you may simply skip them. Now once you have a line in a string, spilt it at spaces, as it seems that they are the delimiters in the given text files. now all you need to do is compare the gateway parts of the two lines. if the are different, send it out.

I wrote a small console utility to help you understand the procedure (The file is also attached):

using System;
using System.Collections.Generic;
using System.IO;


/* This is a small console utility aimed at differentiang delimited data
 * being read from a text file.
 * The parameters are: Column Number, Filename1.txt, FileName2.txt [, FileName3.txt...FileNamen.txt]
 * Author @ Siddharth Dahiya
 */
namespace Difference_Calc
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 2)
            {
                GetFileNames(args);
            }
            else
            {
                Console.WriteLine("Please Provide Names of Files seperated by spaces in this format:");
                Console.WriteLine("Column Number, Filename1.txt, FileName2.txt [, FileName3.txt...FileNamen.txt]"); 
                Console.WriteLine("Press Return to Start");
                string newFileNames = Console.ReadLine();
                if (newFileNames != null && !newFileNames.Equals(""))
                {
                REMOVESPACES:
                    newFileNames = newFileNames.Replace("  ", " ");

                    if (newFileNames.Contains("  "))
                        goto REMOVESPACES;

                    GetFileNames(newFileNames.Split(' '));
                }
            }

            Console.WriteLine("Bye!");
        }

        /// <summary>
        /// Gets the file names.
        /// </summary>
        /// <param name="args">The args.</param>
        private static void GetFileNames(string[] args)
        {
            List<string> files = new List<string>();

            int argIndex = 0, colNum = -1;
            
            foreach (string fileName in args)
            {
                if (argIndex == 0)
                {
                    try
                    {
                        colNum = Convert.ToInt32(fileName);
                    }
                    catch (System.FormatException)
                    {
                        Console.WriteLine("The column number was invalid. Exiting Program...");
                        Environment.Exit(0);
                    }
                    argIndex++;
                } else if (fileName.EndsWith(".txt"))
                {
                    //Verify that the file exists
                    if (File.Exists(fileName))
                        files.Add(fileName);
                    else
                        Console.WriteLine("The file \"" + fileName + "\" was not found. It will not be read for differences");
                } else
                    Console.WriteLine("The file \"" + fileName + "\" was of invalid format. It will not be read for differences");
            }

            //Calculate difference only if there are more than one filename supplied
            if (files.Count > 1)
                PrintDifferences(files, colNum);
            else
            {
                Console.WriteLine("Only one valid file was provided. The program will now quit.");
                Environment.Exit(0);
            }
        }

        /// <summary>
        /// Prints the differences.
        /// </summary>
        /// <param name="Files">The files.</param>
        private static void PrintDifferences(List<string> Files, int columnNumber)
        {
            string title = "Differentiating between ";

            int fileCount = Files.Count, j = 0;

            List<string> gateways = new List<string>(fileCount);

            List<StreamReader> srList = new List<StreamReader>();
    
            foreach (string f in Files)
            {
                //Open file to read stream and add to list
                srList.Add(new StreamReader(f));
                
                //Skip first two lines
                srList[j].ReadLine(); srList[j++].ReadLine();

                //Add file name to the title
                title += f + " & ";
            }

            Console.Title = title.Remove(title.LastIndexOf(" & ")) + ".";

            try
            {
                while (srList.Count > 0)
                {
                    string[] line = new string[fileCount];

                    for (int i = 0; i < fileCount; i++)
                    {
                        if (!srList[i].EndOfStream)
                        {
                            line[i] = srList[i].ReadLine();
                            
                            line[i] = CleanWhiteSpace(line[i]);

                            string gw = "";
                            //Get the gateways
                            try
                            {
                                gw = line[i].Split(' ')[columnNumber];
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Invalid Column Number Provided. Exiting...");
                                Environment.Exit(0);
                            }
                            if (!gateways.Contains(gw))
                                gateways.Add(gw);

                        }
                        else
                        {
                            srList[i].Close();
                            srList.Remove(srList[i]);
                            fileCount--;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error encountered: " + ex.Message);
            }
            finally
            {
                Console.WriteLine("Following are the unique gateways: ");
                
                int i = 1;
                
                foreach (string gw in gateways)
                {
                    Console.WriteLine((i++.ToString() + ":").PadRight(3) + gw);                    
                }

                //Close any streams that might have been left open
                foreach (StreamReader str in srList)
                {
                    str.Close();                    
                }
            }

            Console.Read();
        }

        private static string CleanWhiteSpace(string line)
        {
            do
            {
                line = line.Trim();
                line = line.Replace("  ", " ");
                line = line.Replace('\t', ' ');
                line = line.Replace('\n', ' ');
            } while (line.Contains("  "));

            return line;
        }
    }
}

Sid,
That was good example. Using the logic used in the given example. i was able to create a file with some specific fields exclueded.
Thank you all for the support

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.