Solved

Searching a Text File

Posted on 1999-01-20
19
182 Views
Last Modified: 2010-08-05
I want to search a text file for some formats like
"######", "##/##/##", "##/##/####"
where # indicates a numeric digit. and I want to list
the line numbers and the formats found
to a List view.
0
Comment
Question by:ravink
  • 4
  • 2
  • 2
  • +8
19 Comments
 

Expert Comment

by:omrire
ID: 1469622
you do it with a standad VB code
open "File.name" for input as #1...

if you need a sample tell me.


0
 

Author Comment

by:ravink
ID: 1469623
I know how to open a file and search for an exact text.
But my problem is searching for a format like
"##/##/####" where # is a numeric.
we dont know the exact string, It is a date string.
and I want to get all date strings in that text file.
0
 

Expert Comment

by:PCBOP
ID: 1469624
that's not a question that has dignity enough to be solved,
come on !! can't you even do something that simple ?
forget everything you are thinking and try clean again, maybe you will see
0
 

Expert Comment

by:trillian30
ID: 1469625
Have you tried parsing the text to look for numeric characters and back slashes?

Check the text one character at a time.  When you come to the back slash you can then check the data around it for numeric characters. I don't have the code written - but I think the algorithim should be fairly straight-forward.
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1469626
Try this:

    Dim a$, b$, c$
   
    Open "toto.csv" For Input As #1
   
    Do While Not EOF(1)
        Input #1, a$, b$, c$

        'and convert this string to what you want
        '...
    Loop
   
    Close #1

0
 

Author Comment

by:ravink
ID: 1469627
Chabaud
 That's is not the solution I am looking for.
PCBOP
   If you feel this question is not enough dignified to answer,
then dont answer. But dont make such comments.
Trillian30
  I liked your suggestion.  
I already did tried by parsing the text file and looking for
each charater and checking whether it is a numeric or not
and If it is numeric then started looking for a back slash e.t.c
But the process looked little round about, that's why I put
this question. I expected some kind of easy answer like
using operator 'Like'(not in this context).
Thanks
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1469628
Sorry i have understood your text file was a CSV file with lines that look like
"######", "##/##/##", "##/##/####"
"######", "##/##/##", "##/##/####"
"######", "##/##/##", "##/##/####"
"######", "##/##/##", "##/##/####"
.
0
 
LVL 3

Expert Comment

by:sduckett
ID: 1469629
Hey everybody look at the sort of questions PCBOP has been asking. Tee Hee.
0
 
LVL 3

Expert Comment

by:sduckett
ID: 1469630
further to trillian30 comments - ISDATE() will tell you whether your suspected date actually is one.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Expert Comment

by:ginde
ID: 1469631
You can try a native unix utility, which you can download for Windows, called grep. You can "grep" for your string in any number of files and redirect the output to some file and display it in list view. I am sure, grep gives you the line number too.

But I am not sure if grep will work with your "####" search criteria, it has it's own set of wildcard chars like '?'  for one char and '*' for multiple chars.

If this sounds ok to you, I can help you with grep command.
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1469632
You can use Like to easily to pattern matching:
If S$ Like "###,###,###" then whatever
0
 

Author Comment

by:ravink
ID: 1469633
Hi Mike
The text file is not in the fixed format.
For example the text file contain the following three lines:

 Hi Bob I did this document on 12/12/98 and copied
it to the disk on 12/13/1998. blah, blah, blah .......
010198 then blah...blah.....blah...070798 and e.t.c

The output should be
Line1: 12/12/98
Line2: 12/13/1998
Line3: 010198
Line3: 070798

Hope I am clear this time.
I appreaciate all of your responses.
Thanks.


0
 

Expert Comment

by:trillian30
ID: 1469634
Couldn't you just build S$ by looking for a space, then check if S$ is in one of the following formats:

If S$ Like "######" or S$ Like "##/##/##" or S$ Like "##/##/####" Then ...



0
 

Expert Comment

by:mpenev
ID: 1469635
Start reading one byte at a time and maintaining a buffer, which is 10 characters long

Dim iFileNum As Integer
Dim tBuffer As String
Dim tOne As Byte

        Get #iFileNum, , tOne
        tBuffer = tBuffer & Chr(tOne)
        If Len(tBuffer) > 10 Then
            tBuffer = Mid(tBuffer, 2, 10)
        End If

After that do your "Like" checks for the :
  - last 6 characters
  - if no match - for the last 8 characters
  - if no match - for all 10

Is the idea clear?

0
 
LVL 3

Expert Comment

by:myqlG
ID: 1469636
there is an isdate() function
read in a block and check for idate(blockoftext)
if its true its a date. .if its not..well its not

0
 

Accepted Solution

by:
mpenev earned 100 total points
ID: 1469637
Here is something that worked for me

Test data:
123456wwwewe11/22/33sdfsdfdsf11/22/3333asdfds


Code:



Option Explicit

Const ONLY_DIGITS = "######"
Const SHORT_DATE = "##/##/##"
Const LONG_DATE = "##/##/####"




Public Function IsMatching(ByVal tTestString As String) As Boolean
    Select Case Len(tTestString)
        Case 6
            If tTestString Like ONLY_DIGITS Then
                IsMatching = True
            End If
        Case 8
            If tTestString Like SHORT_DATE Then
                IsMatching = True
            End If
        Case 10
            If tTestString Like LONG_DATE Then
                IsMatching = True
            End If
    End Select
End Function

Private Sub Command1_Click()
Dim iFileNum As Integer
Dim tBuffer As String
Dim tOne As Byte
Dim bNoMatch As Boolean
Dim iLen As Integer

    iFileNum = FreeFile
   
    Open "Test.txt" For Binary As #iFileNum
    While Not EOF(iFileNum)
        Get #iFileNum, , tOne
        tBuffer = tBuffer & Chr(tOne)
        If Len(tBuffer) > 11 Then
            tBuffer = Mid(tBuffer, 2, 11)
        End If
        If InStr("1234567890/", Chr(tOne)) = 0 Then
            bNoMatch = False
            iLen = Len(tBuffer) - 1
            If iLen > 5 Then
                If Not IsMatching(Mid(tBuffer, iLen - 5, 6)) Then
                    If iLen > 7 Then
                        If Not IsMatching(Mid(tBuffer, iLen - 7, 8)) Then
                            If Not IsMatching(Mid(tBuffer, 1, 10)) Then
                                bNoMatch = True
                            Else
                                Debug.Print Mid(tBuffer, 1, 10)
                            End If
                        Else
                            Debug.Print Mid(tBuffer, iLen - 7, 8)
                        End If
                    End If
                Else
                    Debug.Print Mid(tBuffer, iLen - 5, 6)
                End If
            End If
        End If
    Wend
           
    Close #iFileNum
End Sub


0
 
LVL 3

Expert Comment

by:traygreen
ID: 1469638
I think I can expand on what mpenev provided, but in a slightly more inclusive manner.  I take into account eol/eof and other ascii chars as well as allowing for more formats.  Perhaps something in between is what you're looking for

Ran the following code.....
Option Explicit

Public Function RemoveStr(pStrToRemove As String, ByVal pRawStr As String) As String
   Dim lStrPos As Long
   
   lStrPos = InStr(pRawStr, pStrToRemove)
   
   Do While lStrPos > 0
      If lStrPos = 1 Then
         pRawStr = Mid(pRawStr, Len(pStrToRemove) + 1)
      Else
         pRawStr = Mid(pRawStr, 1, lStrPos - 1) & Mid(pRawStr, lStrPos + Len(pStrToRemove))
      End If
      lStrPos = InStr(pRawStr, pStrToRemove)
   Loop
   
   RemoveStr = pRawStr
End Function

Private Function IsBuildingDate(ByVal pStr As String) As Boolean
   ' place your rules here
   Dim sNumStr As String
   Dim bValid As Boolean
   
   sNumStr = RemoveStr("/", pStr)
   bValid = True
   
   If Not IsNumeric(sNumStr) Then
      bValid = False
     
   ElseIf InStr(sNumStr, ".") > 0 Then
      bValid = False
     
   ElseIf InStr(sNumStr, ",") > 0 Then
      bValid = False
     
   ElseIf InStr(sNumStr, " ") > 0 Then
      bValid = False
     
  End If
  IsBuildingDate = bValid
End Function

Private Function GetDate(pFile As Integer, pDateStr As String) As String
     
   Dim sChar As String
   sChar = Input(1, #pFile)
   If Asc(sChar) < 32 Or (Asc(sChar) >= 127 And Asc(sChar) <= 159) Then
      sChar = " "
   End If
   
   
   If IsNumeric(sChar) Or (sChar = "/") Then
      pDateStr = pDateStr & sChar
      pDateStr = GetDate(pFile, pDateStr)
   
   ElseIf sChar = " " And IsDate(pDateStr) Then
      ' no code just trap for 1/1/99 ect
     
   ElseIf (Len(pDateStr) < 6) Then
      pDateStr = ""
   
   ElseIf sChar <> " " Then
      pDateStr = ""
     
   ElseIf (Not IsDate(pDateStr)) And _
          (Not (Len(pDateStr) = 8 And IsDate(Format(pDateStr, "##/##/####")))) And _
          (Not (Len(pDateStr) = 6 And IsDate(Format(pDateStr, "##/##/##")))) Or sChar <> " " Then
      pDateStr = ""
     
   End If
     
   GetDate = pDateStr
End Function

Public Sub ProcessFile()
   Dim iFileNum As Integer
   Dim sChar As String
   
   iFileNum = FreeFile
   
   Open "C:\Test.txt" For Input As #iFileNum
   
   Do While Not EOF(iFileNum)
      sChar = Input(1, #iFileNum)
      If IsNumeric(sChar) Then
         sChar = GetDate(iFileNum, sChar)
         If sChar <> "" Then
            MsgBox sChar
            ' replace message box with what you want to do to the date
         End If
      End If
   Loop
   
   Close #iFileNum
End Sub

Private Sub Command1_Click()
   MsgBox "Get Dates"
   Call ProcessFile
End Sub


against the folowing data......
good 1/1/99
good 1/10/99
good 12011978
bad  120119781    too long
bad  12011978a    too long + alpha
bad  12.01198     decimal
bad  12,120011    comma
bad  12,12011     comma
bad  12,1200.01   comma + decimal
bad  12,20.01     comma + decimal
bad  121219a        alpha
bad  12a1289        alpha
good 120266
bad  1206.1        decimal
good 01/01/1998
bad  01/01/19981  too long
bad  01/01/1998a  alpha
good 02/02/99
good 030399
good 04041999
bad  8  
bad  12345678

0
 
LVL 3

Expert Comment

by:traygreen
ID: 1469639
On running the following file...
this is a test of dates 01/01/1998 and 1/1/1998 and 1/1/98 and 1/12/98 and eol 01/01/98
none of the following dates are good 01/01/1998a or 01/99/1999 or 13/01/1988 or
test 12345678 but 12311988 is good
--------
the date 13/01/1998 came out as good so replace ...
   ElseIf sChar = " " And IsDate(pDateStr) Then
      ' no code just trap for 1/1/99 ect
with ....
   ElseIf sChar = " " And IsDate(pDateStr) Then
      If pDateStr Like "##/##/####" Then
         If Val(Mid(pDateStr, 1, 2)) > 12 Then
            pDateStr = ""
         End If
      End If
     

0
 

Author Comment

by:ravink
ID: 1469640
Thanks for mpenev
and Thanks for Traygreen for adding some more hints to the solution
I really appreciate it.

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now