{"id":2905,"date":"2018-09-06T08:44:51","date_gmt":"2018-09-06T07:44:51","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=2905"},"modified":"2018-09-06T08:44:51","modified_gmt":"2018-09-06T07:44:51","slug":"helper-function-to-replace-data-in-a-csv-file","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2018\/09\/helper-function-to-replace-data-in-a-csv-file\/","title":{"rendered":"Helper function to replace data in a CSV file"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Just a quick share as I needed this for something, this function will replace values in a CSV file. It takes the desired column(s) and value(s) to search for and a new value and desired target column as required parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nfunction update-csvColumn{\n    Param(\n        &#x5B;Parameter(Mandatory=$true)]$csvContents, #input original CSV file contents here (use import-csv first)\n        &#x5B;Parameter(Mandatory=$true)]&#x5B;Array]$searchForColumns, #names of the columns you want to base your search on\n        &#x5B;Parameter(Mandatory=$true)]&#x5B;Array]$searchForValues, #replace rows in $searchForColumn that match these values (in same order!)\n        &#x5B;Parameter(Mandatory=$true)]$replaceColumn, #set this column to what you specified in $newValue\n        &#x5B;Parameter(Mandatory=$true)]$newValue #the new value you wish to set $searchForColumn or $replaceColumn to\n    )\n    if($searchForColumns.Count -ne $searchForValues.Count) {Throw &quot;You must supply an equal number of columns and values to match on&quot;}\n    for($i = 0; $i -lt $csvContents.Count; $i++){\n        $replace = $True\n        for($c = 0; $c -lt $searchForColumns.Count; $c++){\n            if($csvContents&#x5B;$i].$($searchForColumns&#x5B;$c]) -ne $searchForValues&#x5B;$c]){\n                $replace = $False\n            }\n        }\n        if($replace){\n            $csvContents&#x5B;$i].$replaceColumn = $newValue\n        }\n    }\n    return $csvContents\n}\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a quick share as I needed this for something, this function will replace values in a CSV file. It takes the desired column(s) and value(s) to search for and a new value and desired target column as required parameters. function update-csvColumn{ Param( &#x5B;Parameter(Mandatory=$true)]$csvContents, #input original CSV file contents here (use import-csv first) &#x5B;Parameter(Mandatory=$true)]&#x5B;Array]$searchForColumns, #names &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2018\/09\/helper-function-to-replace-data-in-a-csv-file\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Helper function to replace data in a CSV file<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[39],"tags":[],"class_list":["post-2905","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=2905"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2905\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=2905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=2905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=2905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}