Text Parsing 
Author Message
 Text Parsing

I would like to ask for some help parsing text using vb.net and output it to
a Excel file.  Below I have attached a piece of a text file that I would
like to have separated into IP address, Machine name and if it is up or
down.  Once this is created I need to have it then be able to recheck to see
if a machine name has been added or if the machine now shows up or down.

Thanks in advance,

Tony

Host  (128.***.***.*) appears to be down.
Host primus.*****.******.*** (128.***.***.*) appears to be up.
Host 119-valente.*****.*****.*** (128.***.***.**) appears to be down.
Host  (128.***.***.**) appears to be down.



Sun, 06 Feb 2005 23:33:16 GMT  
 Text Parsing
Here's how you can go about parsing each line.  Throw this into a loop that
will read the file and you'll be good to go.

'----------CODE START

Sub ParseLine(ByVal strInLine as String)

  Dim strParseArray As String() = strInLine.Split(" ")

  Dim strHostName As String = strParseArray(1)

  Dim strIPAddress As String = strParseArray(2)
  strIPAddress = strIPAddress.Substring(1, strIPAddress.Length - 2)

  Dim boolHostAlive As Boolean
  If strParseArray(6) = "down." Then
    boolHostAlive = False
  Else
    boolHostAlive = True
  End If

End Sub  'sub ParseLine

'----------CODE END

Hope that helps,
Jeff L.



Mon, 07 Feb 2005 05:08:28 GMT  
 Text Parsing
Tony,

As with many things in .NET there many be more than one
way to solve the problem.  I solution I tried uses Regex.
A regex pattern string of

^\((.*)\).*(down|up)\.$

isolates the IP address and up/down for those lines not
giving the machine name.

^                   Begin a start of string
\(                  Must find (
(                   Start of group
.*                  Gather characters of group
)                   End of group
\)                  Must find )
.*                  Gather characters
(                   Start of group
down|up             Must find up or down
)                   End of group
\.                  Must find .
$                   Must find end of string

  For machine name lines, the regex pattern string is used

^([a-z|A-Z|0-9|\-]*)\..*\((.*)\).*(down|up)\.$

^                   Begin a start of string
(                   Start of group
[a-z|A-Z|0-9|\-]*   Gather alphanumeric or dashes
)                   End of group
\.                  Must find period
.*                  Gather characters
\(                  Must find (
(                   Start of group
.*                  Gather characters of group
)                   End of group
\)                  Must find )
.*                  Gather characters
(                   Start of group
down|up             Must find up or down
)                   End of group
\.                  Must find .
$                   Must find end of string

If you want to use the regex solution, it is best to look
at the first character of the line.  If the charcter is (
then apply the 1st rule, otherwise apply the 2nd rule.  I
used the following test program to valid the pattern
matching strings with your sample data.

Imports System
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Console

Module Module1
    Sub Main()
        Dim astrArgv() As String
        Dim intArgc As Integer
        astrArgv = System.Environment.GetCommandLineArgs
        intArgc = astrArgv.GetUpperBound(0)
        If intArgc <> 2 Then
            WriteLine("<pattern string> <match string>")
            Return
        End If
        Dim expression As Regex
        Try
            expression = New Regex(astrArgv(1))
        Catch
            WriteLine("Bad expression")
            Return
        End Try
        Dim matches As MatchCollection = expression.Matches
(astrArgv(2))
        If matches.Count = 0 Then
            WriteLine("No Match")
        Else
            Dim intMatch As Integer
            Dim groups As GroupCollection
            WriteLine("Matches = {0}", matches.Count)
            groups = matches(0).Groups
            WriteLine("Groups  = {0}", groups.Count)
            For intMatch = 0 To groups.Count - 1
                WriteLine("Group({0}) is '{1}'", intMatch,
groups(intMatch))
            Next
        End If
    End Sub
End Module

Quote:
>-----Original Message-----
>I would like to ask for some help parsing text using

vb.net and output it to
Quote:
>a Excel file.  Below I have attached a piece of a text
file that I would
>like to have separated into IP address, Machine name and
if it is up or
>down.  Once this is created I need to have it then be

able to recheck to see
Quote:
>if a machine name has been added or if the machine now
shows up or down.

>Thanks in advance,

>Tony

>Host  (128.***.***.*) appears to be down.
>Host primus.*****.******.*** (128.***.***.*) appears to
be up.
>Host 119-valente.*****.*****.*** (128.***.***.**) appears
to be down.
>Host  (128.***.***.**) appears to be down.

>.



Mon, 07 Feb 2005 06:13:33 GMT  
 Text Parsing
For some reason the .* got turned into ..* when I gave the
breakdown description of the pattern strings

Pat

Quote:
>-----Original Message-----
>Tony,

>As with many things in .NET there many be more than one
>way to solve the problem.  I solution I tried uses Regex.
>A regex pattern string of

>^\((.*)\).*(down|up)\.$

>isolates the IP address and up/down for those lines not
>giving the machine name.

>^                   Begin a start of string
>\(                  Must find (
>(                   Start of group
>..*                  Gather characters of group
>)                   End of group
>\)                  Must find )
>..*                  Gather characters
>(                   Start of group
>down|up             Must find up or down
>)                   End of group
>\.                  Must find .
>$                   Must find end of string

>  For machine name lines, the regex pattern string is used

>^([a-z|A-Z|0-9|\-]*)\..*\((.*)\).*(down|up)\.$

>^                   Begin a start of string
>(                   Start of group
>[a-z|A-Z|0-9|\-]*   Gather alphanumeric or dashes
>)                   End of group
>\.                  Must find period
>..*                  Gather characters
>\(                  Must find (
>(                   Start of group
>..*                  Gather characters of group
>)                   End of group
>\)                  Must find )
>..*                  Gather characters
>(                   Start of group
>down|up             Must find up or down
>)                   End of group
>\.                  Must find .
>$                   Must find end of string

>If you want to use the regex solution, it is best to look
>at the first character of the line.  If the charcter is (
>then apply the 1st rule, otherwise apply the 2nd rule.  I
>used the following test program to valid the pattern
>matching strings with your sample data.

>Imports System
>Imports System.IO
>Imports System.Text.RegularExpressions
>Imports System.Console

>Module Module1
>    Sub Main()
>        Dim astrArgv() As String
>        Dim intArgc As Integer
>        astrArgv = System.Environment.GetCommandLineArgs
>        intArgc = astrArgv.GetUpperBound(0)
>        If intArgc <> 2 Then
>            WriteLine("<pattern string> <match string>")
>            Return
>        End If
>        Dim expression As Regex
>        Try
>            expression = New Regex(astrArgv(1))
>        Catch
>            WriteLine("Bad expression")
>            Return
>        End Try
>        Dim matches As MatchCollection =
expression.Matches
>(astrArgv(2))
>        If matches.Count = 0 Then
>            WriteLine("No Match")
>        Else
>            Dim intMatch As Integer
>            Dim groups As GroupCollection
>            WriteLine("Matches = {0}", matches.Count)
>            groups = matches(0).Groups
>            WriteLine("Groups  = {0}", groups.Count)
>            For intMatch = 0 To groups.Count - 1
>                WriteLine("Group({0}) is '{1}'",
intMatch,
>groups(intMatch))
>            Next
>        End If
>    End Sub
>End Module

>>-----Original Message-----
>>I would like to ask for some help parsing text using
>vb.net and output it to
>>a Excel file.  Below I have attached a piece of a text
>file that I would
>>like to have separated into IP address, Machine name and
>if it is up or
>>down.  Once this is created I need to have it then be
>able to recheck to see
>>if a machine name has been added or if the machine now
>shows up or down.

>>Thanks in advance,

>>Tony

>>Host  (128.***.***.*) appears to be down.
>>Host primus.*****.******.*** (128.***.***.*) appears to
>be up.
>>Host 119-valente.*****.*****.*** (128.***.***.**)
appears
>to be down.
>>Host  (128.***.***.**) appears to be down.

>>.

>.



Mon, 07 Feb 2005 06:42:47 GMT  
 Text Parsing
I've looked through the help and over the code that you offered and I think
this is really going to be the best way to parse the code.  I got lost when
you did not qualify your variables, i.e. "dim". Then also how were you able
to have it look over the text.  Did you place it into a text file and then
run the code or what?  If you placed it into a text file, where did you
reference the text file.

Thanks,

Tony


Quote:
> Tony,

> As with many things in .NET there many be more than one
> way to solve the problem.  I solution I tried uses Regex.
> A regex pattern string of

> ^\((.*)\).*(down|up)\.$

> isolates the IP address and up/down for those lines not
> giving the machine name.

> ^                   Begin a start of string
> \(                  Must find (
> (                   Start of group
> .*                  Gather characters of group
> )                   End of group
> \)                  Must find )
> .*                  Gather characters
> (                   Start of group
> down|up             Must find up or down
> )                   End of group
> \.                  Must find .
> $                   Must find end of string

>   For machine name lines, the regex pattern string is used

> ^([a-z|A-Z|0-9|\-]*)\..*\((.*)\).*(down|up)\.$

> ^                   Begin a start of string
> (                   Start of group
> [a-z|A-Z|0-9|\-]*   Gather alphanumeric or dashes
> )                   End of group
> \.                  Must find period
> .*                  Gather characters
> \(                  Must find (
> (                   Start of group
> .*                  Gather characters of group
> )                   End of group
> \)                  Must find )
> .*                  Gather characters
> (                   Start of group
> down|up             Must find up or down
> )                   End of group
> \.                  Must find .
> $                   Must find end of string

> If you want to use the regex solution, it is best to look
> at the first character of the line.  If the charcter is (
> then apply the 1st rule, otherwise apply the 2nd rule.  I
> used the following test program to valid the pattern
> matching strings with your sample data.

> Imports System
> Imports System.IO
> Imports System.Text.RegularExpressions
> Imports System.Console

> Module Module1
>     Sub Main()
>         Dim astrArgv() As String
>         Dim intArgc As Integer
>         astrArgv = System.Environment.GetCommandLineArgs
>         intArgc = astrArgv.GetUpperBound(0)
>         If intArgc <> 2 Then
>             WriteLine("<pattern string> <match string>")
>             Return
>         End If
>         Dim expression As Regex
>         Try
>             expression = New Regex(astrArgv(1))
>         Catch
>             WriteLine("Bad expression")
>             Return
>         End Try
>         Dim matches As MatchCollection = expression.Matches
> (astrArgv(2))
>         If matches.Count = 0 Then
>             WriteLine("No Match")
>         Else
>             Dim intMatch As Integer
>             Dim groups As GroupCollection
>             WriteLine("Matches = {0}", matches.Count)
>             groups = matches(0).Groups
>             WriteLine("Groups  = {0}", groups.Count)
>             For intMatch = 0 To groups.Count - 1
>                 WriteLine("Group({0}) is '{1}'", intMatch,
> groups(intMatch))
>             Next
>         End If
>     End Sub
> End Module

> >-----Original Message-----
> >I would like to ask for some help parsing text using
> vb.net and output it to
> >a Excel file.  Below I have attached a piece of a text
> file that I would
> >like to have separated into IP address, Machine name and
> if it is up or
> >down.  Once this is created I need to have it then be
> able to recheck to see
> >if a machine name has been added or if the machine now
> shows up or down.

> >Thanks in advance,

> >Tony

> >Host  (128.***.***.*) appears to be down.
> >Host primus.*****.******.*** (128.***.***.*) appears to
> be up.
> >Host 119-valente.*****.*****.*** (128.***.***.**) appears
> to be down.
> >Host  (128.***.***.**) appears to be down.

> >.



Tue, 08 Feb 2005 05:23:02 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Run Query based on Listbox results - text parsing?

2. Text Parsing

3. Text parsing in VBA

4. Text parsing

5. Text parsing - a new challenge

6. Text Parsing in VBV....

7. Read meta tags via text parsing

8. text parsing

9. text parsing control

10. SQL text parse util

11. lex-like text parsing in VB

12. Text Parsing Routines

 

 
Powered by phpBB® Forum Software