Results 1 to 7 of 7

Thread: edit numbers on lines

  1. #1

    Thread Starter
    Addicted Member ondalor's Avatar
    Join Date
    Jan 2013
    Location
    Washington DC
    Posts
    195

    edit numbers on lines

    how wuuld i make this change throughout an entire text file

    646,"@","800 80061 LIPID PANEL",30.35,"LIPID SCREENING","V77.91"

    646,"@","800 80061-90 LIPID PANEL",30.35,"LIPID SCREENING","V77.91"

    basically everywhere in the file there is a 5 digit number starting with 8 add -90 to the end of it

    make 80061 into 80061-90
    make 81345 into 81345-90
    but dont change 56758 or anything that doesnt start with 8
    Live for the nights you won't remember with the friends you will never forget.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: edit numbers on lines

    As you loop through your text file, you can put the info into a control on your form and then when through, write all the stuff back out to the file.
    You change each line by looking for the 14th character ( I am ASSUMING the "8" will always be the 14th character), and you can use the split() function (for one) and put everything up to the 18th character in the 0th element of your split array, and then simply concatenate a "-90" IF the 14th character is an "8"---if not, simply don't add anything to the 0th array. When you are ready to write back out each line to the file, you can use the 0th and 1st element of your split array to do this.
    You could alse use the Mid() function to see if the 14th char is an "8", and if so, save everything to the left (including) of the 18th character (that is the END of the 5-digit number), concatenate the "-90" to IT, and save everything to the right of the 18th character (excluding it) and then concatenate both strings back together.
    It is simple string manipulation. I PERSONALLY like putting all the lines of my text file into a control (like a listbox), and then overwrite my text file after I have made the string changes...lotsa ways to do this...split is easy, as is Mid. Instr() is another function you could easily use to do this string manipulation.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: edit numbers on lines

    Here's an example of a way to figure it out...using mid()
    I replaced your quotes in each string with nothing, so as to make it easier to work with (initially I set the text property of text1 to one of your strings.
    SO, I clicked on cmd1 to remove the quotes, then clicked on command2 to make the new string.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Text1.Text = Replace(Text1.Text, Chr(34), "")
    
    End Sub
    
    Private Sub Command2_Click()
    Dim myStr1 As String, myStr2 As String, myNewStr1 As String, myFinalStr As String
    myStr1 = Mid(Text1.Text, 1, 15)
    myStr2 = Mid(Text1.Text, 13)
    If Mid(myStr1, 11, 1) = "8" Then
      myNewStr1 = myStr1 & "-90"
    Else
      myNewStr1 = myStr1
    End If
    myFinalStr = myNewStr1 & myStr2
    MsgBox myFinalStr
    End Sub
    Enjoy....
    Out of the net for rest of the day.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: edit numbers on lines

    I think you need to tighten up your requirements a little and define the constraints. Giving an example of one record may not be enough to create a 'universal' solution.
    For example:

    1. Is the item of data you want to inspect and change always within the field following the second comma?
    e.g. could a record look like this:
    Code:
    646,"@","800 56789 LIPID PANEL",30.35,"LIPID SCREENING(80061)","V77.91"
    If so, do you want to change the 80061 to 80061-90?

    2. Will the item always be preceeded by one and only one space character?
    Could it look like
    Code:
    646,"@","800    80061 LIPID PANEL",30.35,"LIPID SCREENING","V77.91" ?
    3. Will there be any other 5 digit numbers within the item / record that will need changing?
    Code:
    646,"@","800 80061 LIPID PANEL(80061)",30.35,"LIPID SCREENING","V77.91"
    or
    Code:
    646,"@","800 80061 LIPID PANEL(80061)",30.35,"LIPID SCREENING 80061","V77.91"
    If that's possible, what do you want to do with the other 80061's ?

    4. Could a record look like:
    Code:
    646,"@","800 56789 LIPID PANEL",80130.35,"LIPID SCREENING","V77.91"
    and if so, would you want the 80130 changed ?

    Ther may be other possible combinations where a 5 digit number may begin with "8", unless you tell us it's going to be difficult to suggest a solution.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: edit numbers on lines

    I'd do it somewhere along these lines

    Step 1: During your loop through the file read each line into a string.
    Use the Split-Function on this string with comma as separator (resulting array called "arrString")
    Step 2: Use the Split-Function again on the 3rd Member of the resulting array "arrString(2)" with space as separator (resulting array called "arrMember")
    Step 3: Check the first character on the 2nd Member of this resulting array "arrMember(1)" if it starts with "8". If Yes add "-90", if not don't.
    Step 4: build your string back together and write it to an output-file
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    Addicted Member
    Join Date
    Jul 2011
    Location
    Mumbai
    Posts
    206

    Re: edit numbers on lines

    you can directly use split with space as separator & check element in splitarray is digit & starting with 8 then add "-90" at end of that element in the splitarray which matches the is digit condition
    NAMO NANASHA

  7. #7
    Lively Member
    Join Date
    May 2009
    Location
    UK
    Posts
    72

    Re: edit numbers on lines

    try this

    Code:
    Option Explicit
    
    Dim LipidText() As String
    
    Private Sub Form_Load()
        ReDim LipidText(0)
    
        Call ReadFile(App.Path & "\" & "LipidFile.txt")
        Call ProcessLipid
        Call WriteFile(App.Path & "\" & "LipidFileNew.txt")
    
    End Sub
    
    Private Sub ProcessLipid()
        Dim ComSep() As String
        Dim LPanel() As String
        Dim LipidLine As String
        Dim NewLPanel As String
    
        Dim i As Long
        
        ' loop through each line in array and process
        For i = 0 To UBound(LipidText)
            LipidLine = LipidText(i)
    
            ' seperate by comma
            ComSep() = Split(LipidLine, ",")
    
            ' seperate Lipid Panel text by space
            LPanel() = Split(ComSep(2), " ")
    
            ' 5 digit text at position 1 of LPanel array
            ' check if we have 8 at the start, if we do - we need to process it
    
            If Left$(LPanel(1), 1) = "8" Then
                ' add -90 to it
                LPanel(1) = LPanel(1) & "-90"
            End If
    
            ' join the LPanel text back into ComSep array
            ComSep(2) = Join(LPanel, " ")
    
            ' restore line and insert into LipidText array
            LipidText(i) = Join(ComSep, ",")
    
        Next i
    
    End Sub
    Private Sub ReadFile(ThisFile As String)
        Dim f As String
        Dim ReadLine As String
    
        f = FreeFile
    
        ' Open file to read
        Open ThisFile For Input As #f
    
        Do Until EOF(f)
            Line Input #f, ReadLine
            ' accept data only, skip empty lines
            If ReadLine <> "" Then
                LipidText(UBound(LipidText)) = ReadLine
                ReDim Preserve LipidText(UBound(LipidText) + 1)
            End If
    
        Loop
    
        Close #f
        ' close the file
        
        ' remove extra array record
        ReDim Preserve LipidText(UBound(LipidText) - 1)
    
        Form1.Caption = "File Read Successful."
    End Sub
    Private Sub WriteFile(ThisFile As String)
        Dim i As Long
        Dim f As String
    
        f = FreeFile
        
        ' open the file to write
        Open ThisFile For Output As #f
    
        ' loop through array and output to file
        For i = 0 To UBound(LipidText)
            Print #f, LipidText(i)
        Next i
    
        Close #f
        ' close the file
        
        ' delete array
        Erase LipidText
    
        Form1.Caption = "File write Successful."
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width