Results 1 to 9 of 9

Thread: [RESOLVED] Need to join lines with just LF at the end

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Resolved [RESOLVED] Need to join lines with just LF at the end

    I've a text file where some lines end with vbLf and I need to join them to become one.

    I already tried several approaches but none gave the result I need (read a line and writing a new one in a new file replacing the vbLf, loading all the lines to a string and replace the vbLf with "", etc...).

    It comes like this
    Name:  Capture.JPG
Views: 271
Size:  17.7 KB

    and I need to change to this
    Name:  Capture.jpg
Views: 259
Size:  5.1 KB

    CEE;AGO;;DBAGOP1;TBAGOCOB;P;"SELECT B.* LF
    FROM DB2PSYS.TBIDRF_MIG A, LF
    DB2PSYS.TBAGOCOB B LF
    WHERE B.IDRBES = A.IDRBES";;OK;;In;108.509;CRLF

    and I need it to be like this

    CEE;AGO;;DBAGOP1;TBAGOCOB;P;"SELECT B.* FROM DB2PSYS.TBIDRF_MIG A, DB2PSYS.TBAGOCOB B WHERE B.IDRBES = A.IDRBES";;OK;;In;108.509;CRLF

    Thank you in advance.

    Cheers

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,322

    Re: Need to join lines with just LF at the end

    The approaches you tried should work if coded properly. Post the code from those attempts and maybe we can help.

    One highly inefficient but simple to implement approach would be:
    -Read file contents into one String
    -Replace vbLF with empty string
    -Replace vbCR with vbCRLF
    Last edited by OptionBase1; Aug 2nd, 2024 at 03:23 PM.

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,678

    Re: Need to join lines with just LF at the end

    How big is the file in question? If it is a reasonable size then you could read the contents into a string and use
    to find and replaced all LineFeeds.

    On my phone so not tested this but something like
    Code:
     
    /(?<!\r)\n
    should work at the regular expression...

    If it doesn't, could you include an example file (work any confidential data removed) and I can try it out properly later.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Re: Need to join lines with just LF at the end

    There are two problems identifying that lines end with vbLf or vbCrLf, and the repalce of the vbLf when lines with it only

    Line read
    sLinha = LineInput(TextFile)

    If sLinha.EndsWith(vbCrLf) Then
    sLinha = sLinha
    Else
    sLinha = sLinha.Substring(0, sLinha.Length - vbLf.Length)
    End If

    Also tried but to no good with

    string.Replace(String.Characters({10}), "")
    regex to identify the LF
    string.Contains

    Another approach was read all the file

    strText = objFile.ReadAl

    strNewText = Replace(strText, vbCrLf, vbCr) Worked
    strNewText = Replace(strText, vbLf, "") Didn't work
    'strNewText = Replace(strText, vbCr, vbCrLf) Worked

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Re: Need to join lines with just LF at the end

    Sample attached
    Attached Files Attached Files

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,678

    Re: Need to join lines with just LF at the end

    Try the following to see if it gets the result you need

    Code:
    Dim data = File.ReadAllText("CSV_LOAD.csv")
    
    Dim regex = New Regex("(?<!\r)\n")
    
    Dim cleanData = regex.Replace(data, "")

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Re: Need to join lines with just LF at the end

    It didn't remove the vbLf.
    The vbLf were replaced by a vbCrLf so the 3 lines didn't become one.

    Attachment 192425

  8. #8
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,678

    Re: Need to join lines with just LF at the end

    I can't access the attachment, really not sure how they are being replaced with a vbCrLF as nothing in the code is adding any vbCrLf characters. I just updated the code above to write the file out...

    Code:
    Dim data = File.ReadAllText("CSV_LOAD.csv")
    Dim regex = New Regex("(?<!\r)\n")
    Dim cleanData = regex.Replace(data, "")
    File.WriteAllText("CSV_OUT.csv", cleanData)
    and the lines certainly aren't looking like they contain any extra vbCrLf, I have attached a screenshot with the original version on the left and the updated version on the right.

    Name:  Untitled.jpg
Views: 48
Size:  18.2 KBi

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2024
    Posts
    26

    Re: Need to join lines with just LF at the end

    Quote Originally Posted by PlausiblyDamp View Post
    I can't access the attachment, really not sure how they are being replaced with a vbCrLF as nothing in the code is adding any vbCrLf characters. I just updated the code above to write the file out...

    Code:
    Dim data = File.ReadAllText("CSV_LOAD.csv")
    Dim regex = New Regex("(?<!\r)\n")
    Dim cleanData = regex.Replace(data, "")
    File.WriteAllText("CSV_OUT.csv", cleanData)
    and the lines certainly aren't looking like they contain any extra vbCrLf, I have attached a screenshot with the original version on the left and the updated version on the right.

    Name:  Untitled.jpg
Views: 48
Size:  18.2 KBi
    This one worked fine.

    Thank you.

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