Share via

Numberformat function and replace it in csv

André Borgeld 431 Reputation points
2020-12-13T11:49:29.17+00:00

Hi everyone,

First question: From the loop I'm calling a function but the result stays empty (newPnum).
Second question: How do i get the results replaced in the CSV
$_.mobile = newPnum?

My CSV

name ;mobile
andre;612345678
michel;0612345678

function numberFormat($pnum){


    $pnum = '{0}-{1} {2} {3} {4}' -f $pnum.Substring(0,2),$pnum.Substring(2,2),$pnum.Substring(4,2), $pnum.Substring(6,2), $pnum.Substring(8)


   }

Import-Csv C:\Temp\import.csv -Encoding UTF8 -Delimiter ';' |


 ForEach-Object{

     $pnum = $null
     $pnum = $_.mobile
     $pnum = $pnum -replace "[^0-9]",""
     $pnumlength = $pnum.length


     if($pnumlength -eq 9)

     {


        $pnum = '0' + $pnum

       $newPnum = numberFormat ($pnum)
       $newPnum

     }

     elseif($pnumlength -eq '10')

     {


           $newPnum = numberFormat ($pnum)
           $newPnum
     }



}
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2020-12-13T16:10:01.27+00:00

I cleaned up your code a bit, but the underlying problem was in the function. You assigned the result of the format operator to a variable which left nothing to be emitted into the pipeline.

function numberFormat($pnum) {
    '{0}-{1} {2} {3} {4}' -f $pnum.Substring(0, 2), $pnum.Substring(2, 2), $pnum.Substring(4, 2), $pnum.Substring(6, 2), $pnum.Substring(8)
}

Import-Csv C:\Temp\import.csv -Encoding UTF8 -Delimiter ';' |
    ForEach-Object {
        $pnum = $_.mobile -replace "[^0-9]", ""
        $pnumlength = $pnum.length

        if ($pnumlength -eq 9)
        {
            numberFormat ('0' + $pnum)
        }
        elseif ($pnumlength -eq '10')
        {
            numberFormat $pnum
        }
    }

Notice that there's no need to surround the arguments in a function call with parentheses. In fact, doing so will cause you headaches, just as separating the arguments with commas will. I left the parens in one function call just to emphasize that adding the '0' was considered to be an expression and not three separate arguments ('0', '+', and $pnum)!

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2020-12-13T16:15:19.797+00:00

    Exporting the data into another CSV needs a bit more data:

    function numberFormat($pnum) {
        '{0}-{1} {2} {3} {4}' -f $pnum.Substring(0, 2), $pnum.Substring(2, 2), $pnum.Substring(4, 2), $pnum.Substring(6, 2), $pnum.Substring(8)
    }
    
    Import-Csv C:\Temp\import.csv -Encoding UTF8 -Delimiter ';' |
        ForEach-Object {
            $pnum = $_.mobile -replace "[^0-9]", ""
            $pnumlength = $pnum.length
    
            if ($pnumlength -eq 9)
            {
                [PSCustomObject]@{
                    Name = $_.Name
                    Mobile = numberFormat ('0' + $pnum)
                }
            }
            elseif ($pnumlength -eq '10')
            {
                [PSCustomObject]@{
                    Name = $_.Name
                    Mobile = numberFormat $pnum
                }
            }
        } | Export-Csv c:\junk\mobile.csv -NoTypeInformation
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. André Borgeld 431 Reputation points
    2020-12-13T18:13:51.917+00:00

    Hey @Rich Matheisen

    Thank you very much for the explanation, tips and tweaking. It's helps a lot.
    I just wanted to look at the PSCustomObject and when to use it in a CSV. Now I know.

    Kind regards,

    Andre

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.