Date format change in a text file

Welcome Forums General PowerShell Q&A Date format change in a text file

Viewing 3 reply threads
  • Author
    Posts
    • #259261
      Participant
      Topics: 1
      Replies: 2
      Points: 23
      Rank: Member

      I have a log file and I need to identify the lines where we have a date and replace those date format with our required format

      Let take an example that we have a line in the log which looks like

      ³3468³AL GOWEFLY³AYSHA ABDULREHMAN³³³³1508701³1975-03-02 00:00:00.000 ³1³³³³³³³³³³2³³5³³³

      I need to find this line and replace this date 1975-03-02 into1975-mar-02

       

      Note: The length of the line and where the date will be in the line is not fixed, it can be anywhere


      I am able to find the lines where we have a date using the below code now, I am unable to understand how we can replace numeric month into words.

      Can someone please help me here?

       

      `

       

      • This topic was modified 4 weeks, 1 day ago by NItesh Bhat.
    • #259270
      Participant
      Topics: 1
      Replies: 2
      Points: 23
      Rank: Member

      I am trying to use Get-Culture).DateTimeFormat.GetAbbreviatedMonthName  to convert the value which is actually working but its seem while replacing the value with the actual value, I am getting below error

      error:

      Cannot convert value “Mar” to type “System.Int32”. Error: “Input string was not in a correct format.”
      At line:1 char:204
      + … 12])”, $1-$((Get-Culture).DateTimeFormat.GetAbbreviatedMonthName($2)) …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvalidCastFromStringToInteger

      At line:1 char:204
      + … 12])”, $1-$((Get-Culture).DateTimeFormat.GetAbbreviatedMonthName($2)) …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvalidCastFromStringToInteger

    • #259288
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      Here’s how I’d do it. It works well and it makes the code quite readable.

      First for the demo we will set up some sample text.

      Now we will make a regex object of the pattern we want to match.

      Next we will make a function to convert the match to the abbreviated month.

      Finally we will process all the lines.

      The output

      Since it works on the entire text you don’t need to put it through a loop, this will speed it up. So you can simply read all the text (if it’s not too large for memory) with Get-Content -Raw . Here is the complete code.

    • #259351
      Participant
      Topics: 1
      Replies: 2
      Points: 23
      Rank: Member

      thanks a lot Doug Maurer

Viewing 3 reply threads
  • You must be logged in to reply to this topic.