2

I have a CSV file I import, and I need to change the format of a column (backed_up_ts) to remove the original data in the column and replace it with the updated format.

The original data for each row in the column looks like this:

Tue Sep 13 07:46:26 MDT 2016 

The first part of my code formats every row so that it can be compared to a date:

$csv = Import-Csv -Path "C:\Users\user\Desktop\NewReport.csv" | Select display_client_name, backed_up_ts
$csv = foreach ($budate in $csv.backed_up_ts) {
$budate = $budate.Substring(3)
$budate = $budate.Replace("MDT", "")
$budate = $budate.Replace("MST", "")
$budate = $budate.Split(" ")
$budate = $budate[1] + " " + $budate[2] + " " + $budate[5]
$budate = ($budate | Get-Date -Format d)

From here I need to take the updated format (in the $budate value) and replace the original data in the $csv.backed_up_ts array.

However, the value of $budate is only the last value of the object in the foreach loop (3/30/16).
The value of $csv.backed_up_ts remains in the array in its original format (Wednesday, August 30th, EST, 2016).

I need the original array $csv.backed_up_ts to be replaced by the coordinating values created by the $budate.

Thanks.

1 Answer 1

2

Updated answer based on clarification of desired transformation of data.

Code

# demonstrate changing columnn data in csv input
$input = @"
header1, header2
value11, "Tue Sep 13 07:46:26 MDT 2016"
value21, "Tue Oct 19 07:46:26 MDT 2017"
"@

$csv = ConvertFrom-Csv $input

"original"
$csv

foreach ($item in $csv)
{
    # modify the header2 column in our csv input
    $parts = $item.header2.Split(' ')
    $newDate = $parts[1] + " " + $parts[2] + " " + $parts[5]
    $item.header2 = [DateTime]::Parse($newDate) | Get-Date -Format d
}

"new"
$csv

Output

original
header1 header2                     
------- -------                     
value11 Tue Sep 13 07:46:26 MDT 2016
value21 Tue Oct 19 07:46:26 MDT 2017
new
value11 9/13/2016                   
value21 10/19/2017    
Sign up to request clarification or add additional context in comments.

5 Comments

So - in trying it with your format, it's not accepting it. let me explain further. The original data in header 2 is "Wed Mar 30 10:03:45 MDT 2016" So in the foreach part - i need to get rid of a lot the data, before i can convert it to a date. foreach ($item in $csv.header2) {$item = $item.replace("MDT", "") This gets the data into a format that can be converted to a date. When i convert it to a date, i then try your code $item.header2 = $item.header2 -replace *, $item But that doesn't work.
Can you update your question or add a comment with what you are trying to accomplish? Perhaps there is a better way. For a value in the format "Wed Mar 30 10:03:45 MDT 2016", what exactly do you want it changed to?
So your code would work - if the -replace parameter would accept a wildcard * - so it would remove everything in that object, and replace it entirely. Also, it can't be replaced by a static '2' - it is replaced by a dynamic variable.
Updated my question. Hopefully it's more understandable.
Updated my answer. Hope this is helpful.

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.