Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Text 2

Status
Not open for further replies.

tiver43809

IS-IT--Management
May 6, 2005
72
US
I don't know much about scripting. I am trying to sort a text file from the last couple of digits of each line. I have gotten a sort to work (kinda) I ahve included the script I have been working with. And at the bottom a small sample of data. Now this sorts my file, but how would I go to the end of line and count back and sort on the s=##? The Sample I included word wrapped. It starts with AD and ends wiht s=# or s=##. It would be ok if the other ones did not get sorted or get written back to file. I can clean them up before I run the script.
ANY help sure would be Appreciated.

Const adVarChar = 200
Const MaxCharacters = 255
Const ForReading = 1
Const ForWriting = 2

Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "ComputerName", adVarChar, MaxCharacters
DataList.Open

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Computers.txt", ForReading)

Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
DataList.AddNew
DataList("ComputerName") = strLine
DataList.Update
Loop

objFile.Close

DataList.Sort = "ComputerName"

DataList.MoveFirst
Do Until DataList.EOF
strText = strText & DataList.Fields.Item("ComputerName") & vbCrLf
DataList.MoveNext
Loop

Set objFile = objFSO.OpenTextFile("C:\Scripts\Computers.txt", ForWriting)

objFile.WriteLine strText
objFile.Close
***sample data
CV
AD,F3,undefined,undefined
AD,F2,undefined,undefined
AD,F1,undefined,undefined
AD,F3, 5,feuds
AD,F4,e=5513,l= 5,n=Oratemarco,acy,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2356,l= 5,n=Orney,poarlene,c=0.00,y=0.00,z=0,o=0,s=6
AD,F4,e=2499,l= 5,n=PXSITING,feudS,c=0.00,y=0.00,z=0,o=0,s=12
AD,F4,e=2344,l= 5,n=PXgnali,RtsaMikdnpo,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2079,l= 5,n=PXsiting,vrgtners,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2202,x=4202,l= 5,n=PXsitng t,PXsit cat,t=feud,w=23rd flr,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=5574,l= 5,n=VollBMiler,Merryl,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2456,x=4456,l= 5,n=BMBER,MikdnBERT,t=feud,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2929,x=4929,l= 5,n=BMMED,CONIxeLTING,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2256,l= 5,n=BMber,Mikdnbert M.,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=210,x=310,l= 10,n=BlackBMll,DaPXd M,t=Plate,c=0.00,y=0.00,z=0,o=0,s=4
AD,F4,e=292,x=392,l= 10,n=Block,DaPXd,t=Plate,c=0.00,y=0.00,z=0,o=0,s=4
AD,F4,e=2039,l= 10,n=Bogomolny,Jude,c=0.00,y=0.00,z=0,o=0,s=9
AD,F4,e=291,x=391,l= 10,n=runnnan,YRtndcia A,t=Plate,c=0.00,y=0.00,z=0,o=0,s=4
AD,F4,e=4103,x=4103,l= 10,n=Lkcci,Gsaegg,c=0.00,y=0.00,z=0,o=0,s=2
AD,F4,e=5594,x=4694,l= 10,n=LuMDKSCO,MICHAEL,c=0.00,y=0.00,z=0,o=0,s=8
AD,F4,e=5545,x=4645,l= 10,n=CHARkcmeS,IxeSAN,t=ASSOC,w=undefined,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2034,x=4034,l= 10,n=MerN,nvmERINE,t=Assoc.,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2708,x=4708,l= 10,n=fpOTE,Rredf,t=ASSC.,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=2744,x=4744,l= 10,n=MDKIG,ANNEMARIE,t=ASSC.,c=0.00,y=0.00,z=0,o=0,s=9
AD,F4,e=282,x=382,l= 10,n=Lucey,ChristophJ,t=Plate,c=0.00,y=0.00,z=0,o=0,s=4
AD,F4,e=2815,x=4815,l= 10,n=Charles,Mikdnse,t=ASSOC,c=0.00,y=0.00,z=0,o=0,s=0
AD,F4,e=239,x=339,l= 10,n=Charters,ElizOrethR,t=Plate,c=0.00,y=0.00,z=0,o=0,s=4
AD,F4,e=2734,l= 10,n=Cho,BMndy,t=Apbl,c=0.00,y=0.00,z=0,o=0,s=14
AD,F4,e=2554,x=4554,l= 10,n=ohriston,Midfaael,t=Assoc.,c=0.00,y=0.00,z=0,o=0,s=7
AD,F4,e=4157,l= 10,n=diebnstein,enodken,c=0.00,y=0.00,z=0,o=0,s=2
 
Easy to understand sorting faq329-4836

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
You can add an index field and sort against it. One way to treat oddly formated filename is used below. You can change the final order per your preference.
[tt]
Const adVarChar = 200
Const MaxCharacters = 255
Const ForReading = 1
Const ForWriting = 2
[blue]Const adUnsignedInteger=19 '4-byte unsigned integer[/blue]

[blue]set rx=new regexp
with rx
.pattern="(s=)(\d+)$"
end with[/blue]

Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "ComputerName", adVarChar, MaxCharacters
[blue]DataList.Fields.Append "sidx", adUnsignedInteger[/blue]
DataList.Open

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFile = objFSO.OpenTextFile("C:\Scripts\Computers.txt", ForReading[red],true[/red])

Do Until objFile.AtEndOfStream
strLine = [blue]trim([/blue]objFile.ReadLine[blue])[/blue]
DataList.AddNew
DataList("ComputerName") = strLine
[blue]if not rx.test(strLine) then
DataList("sidx")=&h7fffffff
else
DataList("sidx")=cint(rx.execute(strLine)(0).submatches(1))
end if[/blue]
DataList.Update
Loop

objFile.Close

'DataList.Sort = "ComputerName"
[blue]DataList.Sort = "sidx"[/blue]

DataList.MoveFirst
Do Until DataList.EOF
strText = strText & DataList.Fields.Item("ComputerName") & vbCrLf
DataList.MoveNext
Loop
DataList.close
set DataList=nothing

Set objFile = objFSO.OpenTextFile("C:\Scripts\Computers.txt", ForWriting[red],true[/red])

objFile.WriteLine strText
objFile.Close
set objFile=nothing
[/tt]
 
I have been doing this first in DOS Edlin, then Edit Then in Notepad for.... Yes you can see how many years. WOOT
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top