LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2005, 09:18 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
checking for duplicate lines in text files (vb.net)


I currently have an application that prints four text box values to a text file. I was wondering of there was anyway to scan the text file before writing to it, to check to see if that entry already exists. Can anybody help me with this?
 
Old 08-01-2005, 09:32 AM   #2
Kdr Kane
Member
 
Registered: Jan 2005
Distribution: SUSE, LFS
Posts: 357

Rep: Reputation: 30
grep

...

Oh, wait! This is a stupid Windows question on Linux Questions!
 
Old 08-01-2005, 09:46 AM   #3
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
What does grep mean
 
Old 08-01-2005, 10:20 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Kdr, the description of the programming forums here clearly states that any sort of programming questions are allowed...

Quote:
This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Also note that it is under the Non-*NIX category...

mrobertson, grep is a utility in Linux that is used specifically for what you are trying to do. It looks for text in a file...

For your solution, you'll probably have to write your own routine. It should be pretty simple, though. Just read in the file line by line and compare to what you are looking for...
 
Old 08-01-2005, 10:31 AM   #5
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
would you be able to give me some sample syntax for how to compare?

Here is the code for how I rread the line in:

Code:
FileOpen(10, "C:\Documents and Settings\alogue\Desktop\Coil PDI\PDI.txt", OpenMode.Append)
        PrintLine(10, txtgradecode.Text & "," & txtwidth.Text & "," & txthbgauge.Text & "," & txttargetgauge.Text)
        FileClose(10)
 
Old 08-01-2005, 10:43 AM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Hmmm.... that looks like the old VB 6 way of working with files. Is there a reason you're not using the System.IO.StreamReader/StreamWriter objects? I would assume they should be available in VB .Net.

Anyway, in C# an easy way of doing this might be something like:

Code:
       public bool TextExistsInFile(string sText, string sFilename)
        {
            bool bResult = false;
            string sLine = null;
            System.IO.StreamReader oReader = null;

            try
            {
                oReader = new System.IO.StreamReader(sFilename);

                while((sLine = oReader.ReadLine()) != null)
                {
                    // How you do the comparison depends on what you want exactly...
                    
                    // Exact comparison
                    if (sLine == sText)
                    {
                        bResult = true;
                        break;
                    }

                    // Compare to front
                    if (sLine.StartsWith(sText))
                    {
                        bResult = true;
                        break;
                    }

                    // Exists anywhere in line
                    if (sLine.IndexOf(sText) >= 0)
                    {
                        bResult = true;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                bResult = false;
            }
            finally
            {
                if (oReader != null)
                {
                    oReader.Close();
                }
            }

            return bResult;
        }
 
Old 08-01-2005, 11:15 AM   #7
Kdr Kane
Member
 
Registered: Jan 2005
Distribution: SUSE, LFS
Posts: 357

Rep: Reputation: 30
I stand corrected.
 
Old 08-01-2005, 11:43 AM   #8
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
How would this be done in vb.net?
 
Old 08-01-2005, 11:57 AM   #9
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
You can do this using the same objects and methods, just format it to be in VB syntax...

No offense, but this is a pretty simple algorithm. A little reading on the .Net documentation is all you should need to do.
 
Old 08-01-2005, 12:02 PM   #10
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
I underdtand the algorithm, I just do not understand how the sText would be defined. I realize that it is the text on the line that I am reading but how would you define it. Same with sLine?
 
Old 08-01-2005, 12:07 PM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
You use that funciton like so:

Code:
if (TextExistsInFile("Text you are looking for in file", "Name of file you are looking for text in"))
{
    // The text exists in the file
}
else
{
   // The text does NOT exist in the file
}
I'm not quite sure how much more clear I can make it...
 
Old 08-01-2005, 12:40 PM   #12
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by mrobertson
Ok! I understand what do do now and I got it working great. Is there anyway that could explain my other thread a bit better. I still do not understand how to define the strings that are being compared. I have a string(Parameter 1) that will be going into a text file(Parameter 2) that needs to compared with everystring already in the text file to see if they match.
Code:
if  not ExistsInFile(Parameter 1, Parameter 2)
   'Append your parameter 1 to the file...
end if
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
Splitting Text lines? 0aniel Programming 9 11-30-2005 03:08 AM
reverse order some lines of text files Melsync Programming 4 09-20-2005 04:40 PM
Removing duplicate lines with sed tireseas Programming 10 01-12-2005 03:27 AM
How 2 find a duplicate word in a text file cowardnewbie Programming 1 09-16-2001 11:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration