skip to Main Content

Revised efforts based on supplied answers:

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> Get-Content ./case.csv  | ForEach-Object ToUpper                            
FJKDLA,W
FKDSLAJF,FDJK;A
NLK;NBF;SDJF,DGDF
VNL;KKDF,BGNGFN
NVCL;V,RGS
NVKL;,THRN
VLKDF,TMMJYMF
FJDK,FDJK;A
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> Get-Content ./case.csv  | ForEach-Object ToLower 
fjkdla,w
fkdslajf,fdjk;a
nlk;nbf;sdjf,dgdf
vnl;kkdf,bgngfn
nvcl;v,rgs
nvkl;,thrn
vlkdf,tmmjymf
fjdk,fdjk;a
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $TextInfo = (New-Object System.Globalization.CultureInfo("en-US")).TextInfo;
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> Get-Content ./case.csv  | ForEach-Object ToTiteCase                         
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> pwsh --version
PowerShell 7.3.4
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 23.04
Release:    23.04
Codename:   lunar
PS /home/nicholas/powershell> 

My main concern is more converting to TitleCase, and, ideally, from the REPL console rather than a script file. I haven’t been able update efforts on targeting a specific column.

By the REPL console I mean the interactive shell, if that makes sense.

All answers and comments have been extremely helpful and appreciated.

additional info:

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> Get-Culture                                        

LCID             Name             DisplayName
----             ----             -----------
1033             en-US            English (United States)

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> (Get-Culture).TextInfo.ToTitleCase($_.fjkdla) 

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> (Get-Culture).TextInfo.ToTitleCase("hmm")     
Hmm
PS /home/nicholas/powershell> 

Which looks to be the desired output for a single string. Not sure how to iterate the CSV file above from the console, however.

2

Answers


  1. Update in response to the now substantially changed question:

    • Your original question, as still reflected in the title, was about CSV data, and targeting a specific column. Given that you originally used Import-Csv, an OO approach was called for, as shown in the next section.

      • To target a specific column you need to know its exact name (except that case doesn’t matter).
      • If your CSV file lacks headers, i.e. an initial line with column names, you need to:
        • Manually supply column names, via Import-Csv‘s -Header parameter, e.g.: Import-Csv ./case.csv -Header Name, Value.
        • And then refer to those column names in a ForEach-Object script block, as shown below (e.g., $_.Name)
    • Your updated question, which uses plain-text processing of your CSV file using Get-Content, seemingly attempts to convert all column values to title case; to that end, use the following approach:

       $textInfo = (Get-Culture).TextInfo
       Get-Content ./case.csv | ForEach-Object { $textInfo.ToTitleCase($_) }
      
       # Alternatively, if performance doesn't matter and you don't want to 
       # create an aux. variable, to reduce the interactive typing effort:
       Get-Content ./case.csv | ForEach-Object { (Get-Culture).TextInfo.ToTitleCase($_) }
      

    Answer to the original, OO question:

    To convert a single, known column to title case in a single pipeline, be sure to use its exact name (though case doesn’t matter); using your sample CSV’s first column name as an example:

    Import-Csv ./case.csv |
      ForEach-Object {
        $_.fjkdla = (Get-Culture).TextInfo.ToTitleCase($_.fjkdla)
        $_ # Output the modified object.
      }
    

    This outputs the modified objects parsed from the CSV file and outputs them directly; by default, they print to the screen; prepend, e.g., $csv = to the pipeline to capture its output in variable $csv.

    Note:

    • If you’ve already imported your CSV file and saved the resulting objects to array $csv, iRon’s suggestion is a simple and efficient solution, using the intrinsic .ForEach() method:

      # Transforms the objects stored in $csv in-place.
      $csv.ForEach({ $_.fjkdla = (Get-Culture).TextInfo.ToTitleCase($_.fjkdla) })
      

    To methodically convert all columns to title case, without needing to know the column (property) values ahead of time, using the intrinsic psobject property:

    Import-Csv ./case.csv |
      ForEach-Object {
        # Iterate over all properties and modify their values.
        foreach ($prop in $_.psobject.Properties) {
          $prop.Value = (Get-Culture).TextInfo.ToTitleCase($prop.Value)
        }
        $_ # Output the modified object.
      }
    

    Note:

    • For simplicity and to minimize the interactive typing effort, the above solutions make a call to Get-Culture to obtain the title-casing method not only for each object, but, in the last case, also for each property of each object, which is inefficient.

    Better-performing alternative, which caches a reference to the method and invokes it with .Invoke():

    $titleCaseMethod = (Get-Culture).TextInfo.ToTitleCase
    Import-Csv ./case.csv |
      ForEach-Object {
        # Iterate over all properties and modify their values.
        foreach ($prop in $_.psobject.Properties) {
          $prop.Value = $titleCaseMethod.Invoke($prop.Value)
        }
        $_ # Output the modified object.
      }
    
    Login or Signup to reply.
  2. The Get-Culture PowerShell cmdlet doesn’t seem to exist on the Replit PowerShell console. It has a TextInfo method you can use to get Title Case, as @mklement shows. The below code worked for me in Replit console, however.

    CSV File:

    Name,Column2
    TEsT tEST TeST,stuff
    tEST2 TesT2 test2,more stuff
    

    REPLIT Console Code:

    $TextInfo = (New-Object System.Globalization.CultureInfo("en-US")).TextInfo;
    
    $csv = Import-Csv -Path 'REPL.csv' | 
        ForEach-Object { 
            $_.Name = $TextInfo.toTitleCase($_.Name)
            $_ 
        }
    
    $csv
    

    Output:

    Name                                       Column2                                  
    ----                                       -------                                  
    Test Test Test                             stuff                               
    Test2 Test2 Test2                          more stuff 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search