1

I have a .txt file that is formatted like this:

---------------------------------------------------------------------------------------------------------------------------------------------------------- 
|Order Number|PegReqOrNo  |Loc |Product Number    |OrdSrtTime|OrdEndTime|Prod. Time|Reqmt Time|OrdSrtDate|OrdEndDate|Comp. Date|Reqmt Date|     Date Var.|
---------------------------------------------------------------------------------------------------------------------------------------------------------- 
|000105812778|            |0002|10000347          |10:03:50  |19:37:43 |19:37:43  |00:00:00  |08/02/2016|02/16/2022|02/16/2022|          |  0/00:00:00|
|000106805034|4200252838  |0002|H827080082GAAZ    |13:43:25  |08:30:04 |08:30:04  |15:00:00  |02/18/2020|09/02/2020|09/02/2020|08/24/2020|   8/17:30:04-|

I am looking to change the strings with pattern ##:##:## in the date var. column, the last on the right. I want to retain the value in front of the / and the - at the end, if there is one.

The two examples from the data above are 0/00:00:00 and 8/17:30:04-, thus I want to retain 0 and -8 respectively.

1
  • You could use Regular Expressions and create a pattern that requires the values to be at the end of the line (meaning there must be 13 times the symbol '|' before it (if i counted correctly). Commented Aug 26, 2020 at 11:52

1 Answer 1

1

For your strange request you can use as follows. I hope I well understood

Private Sub AdjustMyData()
    Try
        Dim allLines As IEnumerable(Of String) = IO.File.ReadAllLines("C:\Users\YourUser\Desktop\test\test.txt")

        Dim data As List(Of String) = (From elemet In allLines).Select(Function(linea As String)
                                                                           If linea.Contains("|") Then
                                                                               Dim parts = linea.Split(CChar("|"))
                                                                               Dim newLinea = (From part In parts
                                                                                               Where part IsNot Nothing
                                                                                               Where part Like "*#/##:##:##*").Select(
                                                                                               Function(s As String) As String
                                                                                                   If s Is Nothing OrElse s.Length = 0 Then Return ""
                                                                                                   linea = linea.Replace(s, "~")
                                                                                                   Dim numerisS As String = CStr(IIf(s.Contains("-"), "-", "")) & Trim(s.Remove(s.IndexOf("/")))
                                                                                                   Return Space(s.Length - numerisS.Length) & numerisS
                                                                                               End Function).ToList.FirstOrDefault
                                                                               Return Strings.Replace(linea, "~", newLinea)
                                                                           End If
                                                                           Return linea
                                                                       End Function).ToList


        IO.File.WriteAllLines("C:\Users\YourUser\Desktop\test\testFinal.txt", data.ToArray)

    Catch ex As Exception
        Console.WriteLine(ex.ToString)
    End Try

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.