Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Search & Replace - Finding 1st Position

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,37664
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Search & Replace - Finding 1st Position
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Search & Replace - Finding 1st Position - 9/12/2006 4:23:20 AM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
Hello,

Im working on creating a script that automatically generates CSV files from SQL data.  Im having an issue of formating the CSV files with my VBScript code.  I want to have 1 record per row, but it appears that some of the data from a few records are ending up on a 2nd line.  Ive tried using the Replace command to remove all New Line and Carriage Returns in the file and adding a New Line before every appearance of "TEST".  Any help is appreciated.

The CSV file is in the format:

TEST00000000001*N/A*7*1121236*...................
TEST00000000002*N/A*2*1223654*...................
TEST00000000003*N/A*4*1323252*...................
TEST00000000004*N/A*7*1333321*...................
TEST00000000005*N/A*1*1121223*...................
containing values 10 and 1.*
TEST00000000006*N/A*3*1122212*...................


 
 
Post #: 1
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 4:27:12 AM   
  ebgreen


Posts: 4970
Score: 31
Joined: 7/12/2005
Status: offline
I think we will need to see your code. That always makes it easier for me to understand what someone is trying to do.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ignignokt)
 
 
Post #: 2
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 4:57:43 AM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
Ill do that shortly.  I think that its a problem with the SQL export of the data.

(in reply to ignignokt)
 
 
Post #: 3
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 6:12:21 AM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
I found out that how Oracle stores the particular data is screwy, so my current VBScript is good.

New question is how can I go about counting the number of a particular character ("*") in a string and removing them from the string??  There should only be 29 "*" as delimiters..any more after the 29th one needs to be deleted.

Here is a fragment of the text file i want to read and edit:

TEST00000000001*N/A*7*1121236*Weather Issues*2**09 Aug 06*TMRP* 
TEST00000000002*N/A*2*1223654*...................
TEST00000000003*N/A*4*1323252*...................
TEST00000000004*N/A*7*1333321*...................
TEST00000000005*N/A*1*1121223*...................
containing values 10 and 1.*
TEST00000000006*N/A*3*1122212*...................

(in reply to ignignokt)
 
 
Post #: 4
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 6:17:19 AM   
  ebgreen


Posts: 4970
Score: 31
Joined: 7/12/2005
Status: offline
Oooh...this will be a tricky one. I'll try to come up with a good way and in the meantime someone like ehvbs will come along with a better solution than I'll come up with and he'll have a two page summary on how it works. 

In the meantime though, I'd like to make sure that I have the situation correct. You have a string (well several of them one on each line but we'll just look at one). This string will have at least 29 * characters in it. Any * characters after the 29th one need to be removed. Is this correct?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ignignokt)
 
 
Post #: 5
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 6:25:53 AM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
That is correct.  Any * after the 29th one in the row should be deleted.  Just some background info about this:  Each line is a row from a database.  Each field in that row seperated by a *.  There should only be 30 fields or 29 *'s in one line.  I appreciate the help!

(in reply to ebgreen)
 
 
Post #: 6
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 6:35:40 AM   
  ebgreen


Posts: 4970
Score: 31
Joined: 7/12/2005
Status: offline
Will there be any other data mixed in there with the 30th+ *s or would it be safe to simply delete everything from the 30th * on (including the 30th * itself)?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ignignokt)
 
 
Post #: 7
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 6:42:17 AM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
There is data after the 30th *'s...And I want to keep it.  The only thing to be deleted would be *'s.  I guess a way of doing it would be counting the number of *'s and after the count = 30, then Replace each * reached with a " " (Space)

(in reply to ebgreen)
 
 
Post #: 8
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 6:46:02 AM   
  ebgreen


Posts: 4970
Score: 31
Joined: 7/12/2005
Status: offline
If this is true:

quote:

Each line is a row from a database.  Each field in that row seperated by a *.  There should only be 30 fields or 29 *'s in one line.


Then how is there data after the 30th *? Wouldn't that imply more than 30 fields in the DB record?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ignignokt)
 
 
Post #: 9
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 6:55:37 AM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
Thats what is screwy..In my SQL, I only SELECT 30 fields and i use the * to seperate each column.  Well for some reason, in one field, there are 2 records that have 2000+ characters and its causing the output to have * all thru out the actually data.  So for the majority of the lines outputted by my VBScript, they have 29 *'s...but for 2 lines, there are like 50 *'s...dont know if i explained that clearly enough lol

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 7:05:39 AM   
  ebgreen


Posts: 4970
Score: 31
Joined: 7/12/2005
Status: offline
Ok,see if this will work. You will want to call it with each line that you want to clean up and it will return the cleaned up line. If the variable holding the line is called strLine, then you would do this:

strCleaned = TrimExtraDelimiters(strLine, 30)
Function TrimExtraDelimiters(strOriginal, nFields)
   Dim arrParts
   Dim strConCat
   Dim i
  
   arrParts = Split(strOriginal, "*")
   If UBound(arrParts) < nFields Then
       TrimExtraDelimiters = strOriginal
       Exit Function
   End If
   For i = nFields To UBound(arrParts)
       strConCat = strConCat & arrParts(i)
   Next
   ReDim Preserve arrParts(nFields)
   arrParts(UBound(arrParts)) = strConCat
   TrimExtraDelimiters = Join(arrParts, "*")
End Function

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ignignokt)
 
 
Post #: 11
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 10:36:21 PM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
Thanks for your help.

Im getting a Type Mismatch error where I have the line:  strCleaned = Trim(strStarter, 30).  Below is my code:

dim o
dim p
dim fs
dim strStarter
dim strCleaned
dim strOriginal
dim nFields
Const ForReading = 1
Const ForWriting = 2

set fs = CreateObject("Scripting.FileSystemObject")
set o = fs.OpenTextFile("C:\temp\t2.txt", ForReading, False)
set p = fs.CreateTextFile("C:\temp\t3.txt", True)
Do While o.AtEndOfStream <> True
    strStarter = o.ReadLine
    strStarter = Replace(strStarter, "   ", "")
    strStarter = Replace(strStarter, vbCr, "")
    strStarter = Replace(strStarter, vbCr, "")
    strStarter = Replace(strStarter, vbTab, "")
    strStarter = Replace(strStarter, vbLf, "")
    strStarter = Replace(strStarter, vbCrLf, "")
    strStarter = Replace(strStarter, vbNewLine, "")
    strStarter = Replace(strStarter, vbCrNullChar, "")
    strStarter = Replace(strStarter, "TEST", vbNewLine & "TEST")

    strCleaned = Trim(strStarter, 30)
    p.Write(strCleaned)
Loop

o.Close
p.Close


Sub Trim (strOriginal, nFields)

dim arrParts
dim strConCat
dim i

arrParts = Split(strOriginal, "*")

If UBound(arrParts) < nFields THen
    Trim = strOriginal
    Exit Sub
End If

For i = nFields to UBound(arrParts)
    strConCat = strConCat&arrParts(i)
Next

ReDim Preserve arrParts(nFields)
arrParts(UBound(arrParts)) = strConCat
Trim = Join(arrParts, "*")

End Sub

(in reply to ignignokt)
 
 
Post #: 12
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 10:53:39 PM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
Disregard that last post :) Im an ****.  Used SUB in place of Function. When I changed it, it ran fine.  The problem is that it isnt deleting any of the extra *'s.  Should I post the data file?

(in reply to ignignokt)
 
 
Post #: 13
 
 RE: Search & Replace - Finding 1st Position - 9/12/2006 11:52:04 PM   
  ignignokt

 

Posts: 31
Score: 0
Joined: 9/12/2006
Status: offline
Here is the original text. (Sorry, cant upload files yet..):

TEST00005453*martin                   *       1153837484*TC-CUST-SPT-SVCDESK        *fraz                      *   1156963870*         2*NH Paris                                                                                                                         *7/25/2006 10:24:45                      *1153837484martinSGT Cruz supposedly cannot access the net from Paris. *          1*               *STH TC                                                 *STHCM                                                    *Cruz                                                                                                                                                                                                                                                  *TACTICAL                                                            *           *                              *                                                                                                                                                                                                                                                          *          *TC-NETOPS                                      *N *     *          *               *                         *                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                *1153837485*                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                *
TEST00005506*mim                         *       1157031305*TC-SPTOPS-DESKTOP-SPT      *mim                         *   1157032728*         2*Connectivity Request                                                                                                            *8/31/2006 09:35:06                      *1157031305mimNeed verify how many phone lines are established for OT 41 AND *          2*               *STH TC                                                 *STHCM                                                    *Stephens                                                                                                                                                                                                                                              *TELEPHONE SUPPORT                                                   *           *                              *                                                                                                                                                                                                                                                          *          *TC-CUST-SPT-SVCDESK                            *N *     *          *OT-41 OT-42    *                         *                                                                                                                                                                                                                                                                                                                                                                                                                                                                          &nb