check first column in Bar delimeted file for Null or Empty and replace

 I have a file with no header info just data values that is delimited by a veritcal bar. The file is saved as .csv.

pay.csv

 CAN|2000|inv3002|300.00

 |2000|inv3002|300.00

AFter

BEG|2000|inv3002|300.00

Sometimes when the file is created the first field is empty or Null. I want to find these and Insert a constant

value of 'BEG' and kept the remaining values in file.

 Thanks.

October 23rd, 2013 12:34pm

Something like this should work:

(Get-Content .\pay.csv) -replace '^\|', 'BEG|' | Set-Content .\pay.csv

  • Proposed as answer by DScripter 15 hours 54 minutes ago
Free Windows Admin Tool Kit Click here and download it now
October 23rd, 2013 1:24pm

  Awesome!!!!  Can it report how many changes happended?

 Fixed=?
 NoChange=?

  Like doing a write-host or something to report back.

 THanks.

October 23rd, 2013 4:44pm

Sure, you could do something like that:

$unchanged = 0
$changed = 0

(Get-Content .\pay.csv) |
ForEach-Object {
    if ($_ -match '^\|')
    {
        $changed++
        'BEG' + $_
    }
    else
    {
        $unchanged++
        $_
    }
} |
Set-Content .\pay.csv

Write-Host "Lines changed: $changed, Lines unchanged: $unchanged."

  • Proposed as answer by Mike Laughlin 13 hours 36 minutes ago
  • Marked as answer by hart60 9 hours 9 minutes ago
Free Windows Admin Tool Kit Click here and download it now
October 23rd, 2013 4:50pm

 Thanks.

October 23rd, 2013 9:26pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics