Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Search text file and list computers which belong under same headin

Reply
 
Old 06-02-2009   #1 (permalink)
Jaz


 
 

Search text file and list computers which belong under same headin

Hi,
I have a text file as below which contains headings (starts with Group
with computer information below them. The file contains computers at
different lines of the text file which have the same heading name and I want
to bring these together so that there will be unique headings with all the
computers below them which belong to that group.
I would also like to sort the file based ont the heading name.
I have been working on this for a while and have got nowhere near what I
need.

The file is as below:

Group:System Drives 3;
wserversql04.test.net; All; Friday Full Daily Incremental;
Group: Web Servers;
serverdb01.test.net; All; Thursday Full Daily Incremental;
Group: Web Servers;
servers07.test.net; All; Thursday Full Daily Incremental;
Group: System Drives 3;
serveroc02.test.net; All; Thursday Full Daily Incremental;
Group: System Drives 4;
serverelc01.test.net; All; Tuesday Full Daily Incremental;
Group:MSSQL Database 3;
serversql11.test.net; "MSSQL Full Every Day;
Group: System Drives 4;
serverfle02.test.net; All; Thursday Full Daily Incremental;
Group: System Drives 4;
serverpsa96.test.net; All; Thursday Full Daily Incremental;
Group: System Drives 4;
serverpac01.test.net; All; Wednesday Full Daily Incremental;
Group:WSERVERCTI02 MSSQL;
wservercti02.test.net; "MSSQL Full Every Day;
Group:ServerCTI02 System Drives;
servercti02.test.net; All; Tuesday Full Daily Incremental;
Group:System Drives 3;
serversql11.test.net; All; Friday Full Daily Incremental;
Group:MSSQL Database 3;
serveroc02.test.net; "MSSQL Full Every Day;
Group: System Drives 3;
serversim01.test.net; All; Tuesday Full Daily Incremental;

The script I have so far is:

Const ForReading = 1
Const ForWriting = 2
Const objOutputFile1 = "Groupconfig3.txt" 'This is the file above
Dim objFSO
Dim objTextFile,objTextOuput1
Dim StrLine, StrLine2
Dim StrCompleted, StrGroupname, StrSaveset, StrSchedule, StrClientname
Dim intRtn, intRtn2, ncount
dim sampletext, objRegExp, SearchPattern, ReplacePattern, matches

'open text file

sampletext = "Group:"

'// create a new instance of the regular expression object
Set objRegExp = New RegExp

'objRegExp.Pattern = searchpattern ' apply the search pattern
objRegExp.Global = True ' match all instances of the search pattern
objRegExp.IgnoreCase = True ' ignore Case

'// find all occurences of the search pattern in the sample text
Set matches = objRegExp.execute(sampletext)

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile _
("Groupconfig.txt", ForReading)

Set objTextOuput1 = objFSO.CreateTextFile(objOutputFile1, ForWriting,True)

Do Until objFile.AtEndOfStream
StrLine = objfile.readline
If matches.Count > 0 Then ' there was at least one match to the search pattern
For Each match in matches
intcheck = StrComp(StrLine,StrLine2,vbtextcompare)
If intcheck <> 0 Then
objTextOuput1.Write StrLine & vbNewLine
End If
StrLine2 = StrLine
Next
Else ' there were no matches found
objTextOuput1.Write StrLine & " Not found" & vbNewLine
End If

Loop
'// releast the reg exp object
Set objRegExp = Nothing

--
Jaz

My System SpecsSystem Spec
Old 06-02-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Search text file and list computers which belong under same headin


"Jaz" <Jaz@xxxxxx> wrote in message
news:421FAE83-FDD8-43EA-A09F-F09CDEFC1D55@xxxxxx
Quote:

> Hi,
> I have a text file as below which contains headings (starts with Group
> with computer information below them. The file contains computers at
> different lines of the text file which have the same heading name and I
> want
> to bring these together so that there will be unique headings with all the
> computers below them which belong to that group.
> I would also like to sort the file based ont the heading name.
> I have been working on this for a while and have got nowhere near what I
> need.
>
> The file is as below:
>
> Group:System Drives 3;
> wserversql04.test.net; All; Friday Full Daily Incremental;
> Group: Web Servers;
> serverdb01.test.net; All; Thursday Full Daily Incremental;
> Group: Web Servers;
> servers07.test.net; All; Thursday Full Daily Incremental;
> Group: System Drives 3;
> serveroc02.test.net; All; Thursday Full Daily Incremental;
> Group: System Drives 4;
> serverelc01.test.net; All; Tuesday Full Daily Incremental;
> Group:MSSQL Database 3;
> serversql11.test.net; "MSSQL Full Every Day;
> Group: System Drives 4;
> serverfle02.test.net; All; Thursday Full Daily Incremental;
> Group: System Drives 4;
> serverpsa96.test.net; All; Thursday Full Daily Incremental;
> Group: System Drives 4;
> serverpac01.test.net; All; Wednesday Full Daily Incremental;
> Group:WSERVERCTI02 MSSQL;
> wservercti02.test.net; "MSSQL Full Every Day;
> Group:ServerCTI02 System Drives;
> servercti02.test.net; All; Tuesday Full Daily Incremental;
> Group:System Drives 3;
> serversql11.test.net; All; Friday Full Daily Incremental;
> Group:MSSQL Database 3;
> serveroc02.test.net; "MSSQL Full Every Day;
> Group: System Drives 3;
> serversim01.test.net; All; Tuesday Full Daily Incremental;
>
> The script I have so far is:
>
> Const ForReading = 1
> Const ForWriting = 2
> Const objOutputFile1 = "Groupconfig3.txt" 'This is the file above
> Dim objFSO
> Dim objTextFile,objTextOuput1
> Dim StrLine, StrLine2
> Dim StrCompleted, StrGroupname, StrSaveset, StrSchedule, StrClientname
> Dim intRtn, intRtn2, ncount
> dim sampletext, objRegExp, SearchPattern, ReplacePattern, matches
>
> 'open text file
>
> sampletext = "Group:"
>
> '// create a new instance of the regular expression object
> Set objRegExp = New RegExp
>
> 'objRegExp.Pattern = searchpattern ' apply the search pattern
> objRegExp.Global = True ' match all instances of the search pattern
> objRegExp.IgnoreCase = True ' ignore Case
>
> '// find all occurences of the search pattern in the sample text
> Set matches = objRegExp.execute(sampletext)
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
>
> Set objFile = objFSO.OpenTextFile _
> ("Groupconfig.txt", ForReading)
>
> Set objTextOuput1 = objFSO.CreateTextFile(objOutputFile1, ForWriting,True)
>
> Do Until objFile.AtEndOfStream
> StrLine = objfile.readline
> If matches.Count > 0 Then ' there was at least one match to the search
> pattern
> For Each match in matches
> intcheck = StrComp(StrLine,StrLine2,vbtextcompare)
> If intcheck <> 0 Then
> objTextOuput1.Write StrLine & vbNewLine
> End If
> StrLine2 = StrLine
> Next
> Else ' there were no matches found
> objTextOuput1.Write StrLine & " Not found" & vbNewLine
> End If
>
> Loop
> '// releast the reg exp object
> Set objRegExp = Nothing
>
> --
> Jaz
Assuming that you want to have an output like this:
Group: System Drives 3;
serveroc02.test.net; All; Thursday Full Daily Incremental;
serversim01.test.net; All; Tuesday Full Daily Incremental;
Group: System Drives 4;
serverelc01.test.net; All; Tuesday Full Daily Incremental;
serverfle02.test.net; All; Thursday Full Daily Incremental;
serverpac01.test.net; All; Wednesday Full Daily Incremental;
serverpsa96.test.net; All; Thursday Full Daily Incremental;
Group: Web Servers;
serverdb01.test.net; All; Thursday Full Daily Incremental;
Group: Web Servers;
servers07.test.net; All; Thursday Full Daily Incremental;
Group:MSSQL Database 3;
serveroc02.test.net; "MSSQL Full Every Day;
serversql11.test.net; "MSSQL Full Every Day;
Group:ServerCTI02 System Drives;
servercti02.test.net; All; Tuesday Full Daily Incremental;
Group:System Drives 3;
serversql11.test.net; All; Friday Full Daily Incremental;
wserversql04.test.net; All; Friday Full Daily Incremental;
Group:WSERVERCTI02 MSSQL;
wservercti02.test.net; "MSSQL Full Every Day;

I would use the following approach:
1. Read the whole file into an array like so:
- Element 0: Group name (must never be blank)
- Element 1: Server name (will often be blank)
2. Sort the array by Group name.
3. Walk through the array:
- WriteLine the Group name if it differs from its predecessor
- WriteLine the Server name if it is not blank.


My System SpecsSystem Spec
Old 06-02-2009   #3 (permalink)
ekkehard.horner


 
 

Re: Search text file and list computers which belong under same headin

Jaz schrieb:
Quote:

> Hi,
> I have a text file as below which contains headings (starts with Group
> with computer information below them. The file contains computers at
> different lines of the text file which have the same heading name and I want
> to bring these together so that there will be unique headings with all the
> computers below them which belong to that group.
> I would also like to sort the file based ont the heading name.
> I have been working on this for a while and have got nowhere near what I
> need.
[...]

(1) Think "Dictionary", if you need a hierarchical datastructure
(2) Process a file line by line, if you are not *forced* to do
otherwise
(3) Use .Net ArrayList, if you need an one-dimensional array sorted

These principles in code:

Const bVerbose = False

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpecIn : sFSpecIn = ".\groupservers.txt"
Dim dicGrps : Set dicGrps = CreateObject( "Scripting.Dictionary" )
Dim reGrp : Set reGrp = New RegExp
reGrp.Pattern = "Group:\s*([^;]+);"
Dim reClean : Set reClean = New RegExp
reClean.Global = True
reClean.Pattern = ";\s+"
Dim sCleanRpl : sCleanRpl = ";"

Dim tsIn : Set tsIn = oFS.OpenTextFile( sFSpecIn )
Dim sLine, oMTS, sGroup, sServer, aServer
Do Until tsIn.AtEndOfStream
sLine = tsIn.ReadLine()
If bVerbose Then WScript.Echo "R", sLine
Set oMTS = reGrp.Execute( sLine )
If 1 = oMTS.Count Then ' found group line
sGroup = oMTS( 0 ).SubMatches( 0 )
If bVerbose Then WScript.Echo "G", sGroup
If Not dicGrps.Exists( sGroup ) Then
If bVerbose Then WScript.Echo "A", sGroup
Set dicGrps( sGroup ) = CreateObject( "Scripting.Dictionary" )
End If
Else
sServer = reClean.Replace( Trim( sLine ), sCleanRpl )
aServer = Split( sServer, ";" )
If 1 < UBound( aServer ) Then
sServer = aServer( 0 )
If bVerbose Then WScript.Echo "S", sServer
dicGrps( sGroup )( sServer ) = aServer
Else
WScript.Echo "?", sLine
End If
End If
Loop
tsIn.Close

WScript.Echo "======================"

Dim aSortedGrps : Set aSortedGrps = sortDNAL( dicGrps.Keys )

For Each sGroup In aSortedGrps
WScript.Echo "Group:", sGroup
For Each sServer In dicGrps( sGroup ).Keys
WScript.Echo "", sServer, vbTab, Join( dicGrps( sGroup )( sServer ), "|" )
Next
WScript.Echo
Next

'' return sorted .NET ArrayList for one-dimensional array
Function sortDNAL( aD1 )
Dim oDNAL : Set oDNAL = CreateObject( "System.Collections.ArrayList" )
Dim nIdx
For nIdx = 0 To UBound( aD1 )
oDNAL.Add aD1( nIdx )
Next
oDNAL.Sort
Set sortDNAL = oDNAL
End Function


output (bVerbose = False)

=== groupServers: display grouped servers =====================================
======================
Group: MSSQL Database 3
serversql11.test.net serversql11.test.net|"MSSQL Full Every Day|
serveroc02.test.net serveroc02.test.net|"MSSQL Full Every Day|

Group: ServerCTI02 System Drives
servercti02.test.net servercti02.test.net|All|Tuesday Full Daily Incremental|

Group: System Drives 3
wserversql04.test.net wserversql04.test.net|All|Friday Full Daily Incremental|
serveroc02.test.net serveroc02.test.net|All|Thursday Full Daily Incremental|
serversql11.test.net serversql11.test.net|All|Friday Full Daily Incremental|
serversim01.test.net serversim01.test.net|All|Tuesday Full Daily Incremental|

Group: System Drives 4
serverelc01.test.net serverelc01.test.net|All|Tuesday Full Daily Incremental|
serverfle02.test.net serverfle02.test.net|All|Thursday Full Daily Incremental|
serverpsa96.test.net serverpsa96.test.net|All|Thursday Full Daily Incremental|
serverpac01.test.net serverpac01.test.net|All|Wednesday Full Daily Incremental|

Group: Web Servers
serverdb01.test.net serverdb01.test.net|All|Thursday Full Daily Incremental|
servers07.test.net servers07.test.net|All|Thursday Full Daily Incremental|

Group: WSERVERCTI02 MSSQL
wservercti02.test.net wservercti02.test.net|"MSSQL Full Every Day|

=== groupServers: 0 done (00:00:02) ===========================================
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Search and Replace text file very slow PowerShell
use a list of email addresses from text file PowerShell
send list of AD computersin OU to text file PowerShell
Search for Text in UTF-8 File Vista General
Search and replace in a text file? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46