Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell Replace String CSV column

I am new to Powershell and I have tried a few different code adjustments but keep getting stuck.

I have a CSV file from 3rd party application that I need to replace a string

host_display_name serice_display_name service_output
Server1 Disk / DISK OK – free space: / 16566 MB (86% inode=96%);
Server1 Disk /Logs DISK OK – free space: / 16566 MB (86% inode=96%);
Server1 mem OK – 25.7% (1023324 kB) used.
Server2 Disk / DISK OK – free space: / 16566 MB (76% inode=86%);
Server2 Disk /Logs DISK OK – free space: / 16566 MB (56% inode=56%);
Server2 mem OK – 25.7% (1023324 kB) used.

I would like to update it to show

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

host_display_name serice_display_name service_output
Server1 Disk / DISK OK – free space: / 16566 MB (86% FREE)
Server1 Disk /Logs DISK OK – free space: / 16566 MB (66% FREE)
Server1 mem OK – 25.7% (1023324 kB) used.
Server2 Disk / DISK OK – free space: / 16566 MB (76% FREE)
Server2 Disk /Logs DISK OK – free space: / 16566 MB (56% FREE)
Server2 mem OK – 25.7% (1023324 kB) used.

Here is my Powershell Code

import-csv C:\Projects\Excel_Data_Powershell\Output.csv  | foreach { If($_.ou -match “inode=*%”) {$_.OU -replace “inode=*%”,”FREE”}} | export-csv C:\Projects\Excel_Data_Powershell\OutputUPDATED.csv

>Solution :

You could use a calculated property with Select-Object to recreate the service_output column based on your condition to replace the values inside the parentheses. For replacing them you can use -replace, as for the regex being used it’s likely to be improved but this seems to do the trick. See https://regex101.com/r/TrDBXu/1 for the details.

Import-Csv C:\Projects\Excel_Data_Powershell\Output.csv | Select-Object *, @{
    Name = 'service_output'
    Expression = { $_.service_output -replace '(\(\d+%).*', '$1 FREE)' }
} -ExcludeProperty service_output |
Export-Csv C:\Projects\Excel_Data_Powershell\OutputUPDATED.csv -NoTypeInformation
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading