Powershell Change data in a CSV's column, that has no headers









up vote
0
down vote

favorite












I need to change a CSV column, that has no headers.



Here's my test data WITH HEADERS:


data.csv:



order|color|shape
1|green|square
1|red|rectangle


And here's what i used to change the first column to 0.



$ImportedCSV = Import-CSV data.csv -Delimiter '|'
$NewCSV = Foreach ($Entry in $ImportedCsv)
Switch ($Entry."order")
1 $Entry."order" = "0"

$Entry

$NewCSV | Export-CSV done.csv -Delimiter '|' -NoTypeInformation

(Get-Content -Path done.csv ).Replace('"','') | Set-Content -Path done.csv


The result is this:



order|color|shape
0|green|square
0|red|rectangle


My data.csv wont have headers to start with.


Can "Switch ($Entry."order")" use column numbers rather than the header name?

So, something like "Column 1" rather then "order"?



Please no mention of simply adding the headers then delete the headers when done.

In the end, i need to change a column's data based on column number, rather than header name.


So...replace ($Entry."order") with what?


($Entry.Column 1) = nope

($Entry.Column-1) = nope


Thanks for any help.










share|improve this question



















  • 1




    the Import-CSV cmdlet has a -Header parameter that will allow you to set your header line when the file itself has none. take a look at Get-Help Import-CSV for more info. [grin]
    – Lee_Dailey
    Nov 9 at 22:54














up vote
0
down vote

favorite












I need to change a CSV column, that has no headers.



Here's my test data WITH HEADERS:


data.csv:



order|color|shape
1|green|square
1|red|rectangle


And here's what i used to change the first column to 0.



$ImportedCSV = Import-CSV data.csv -Delimiter '|'
$NewCSV = Foreach ($Entry in $ImportedCsv)
Switch ($Entry."order")
1 $Entry."order" = "0"

$Entry

$NewCSV | Export-CSV done.csv -Delimiter '|' -NoTypeInformation

(Get-Content -Path done.csv ).Replace('"','') | Set-Content -Path done.csv


The result is this:



order|color|shape
0|green|square
0|red|rectangle


My data.csv wont have headers to start with.


Can "Switch ($Entry."order")" use column numbers rather than the header name?

So, something like "Column 1" rather then "order"?



Please no mention of simply adding the headers then delete the headers when done.

In the end, i need to change a column's data based on column number, rather than header name.


So...replace ($Entry."order") with what?


($Entry.Column 1) = nope

($Entry.Column-1) = nope


Thanks for any help.










share|improve this question



















  • 1




    the Import-CSV cmdlet has a -Header parameter that will allow you to set your header line when the file itself has none. take a look at Get-Help Import-CSV for more info. [grin]
    – Lee_Dailey
    Nov 9 at 22:54












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to change a CSV column, that has no headers.



Here's my test data WITH HEADERS:


data.csv:



order|color|shape
1|green|square
1|red|rectangle


And here's what i used to change the first column to 0.



$ImportedCSV = Import-CSV data.csv -Delimiter '|'
$NewCSV = Foreach ($Entry in $ImportedCsv)
Switch ($Entry."order")
1 $Entry."order" = "0"

$Entry

$NewCSV | Export-CSV done.csv -Delimiter '|' -NoTypeInformation

(Get-Content -Path done.csv ).Replace('"','') | Set-Content -Path done.csv


The result is this:



order|color|shape
0|green|square
0|red|rectangle


My data.csv wont have headers to start with.


Can "Switch ($Entry."order")" use column numbers rather than the header name?

So, something like "Column 1" rather then "order"?



Please no mention of simply adding the headers then delete the headers when done.

In the end, i need to change a column's data based on column number, rather than header name.


So...replace ($Entry."order") with what?


($Entry.Column 1) = nope

($Entry.Column-1) = nope


Thanks for any help.










share|improve this question















I need to change a CSV column, that has no headers.



Here's my test data WITH HEADERS:


data.csv:



order|color|shape
1|green|square
1|red|rectangle


And here's what i used to change the first column to 0.



$ImportedCSV = Import-CSV data.csv -Delimiter '|'
$NewCSV = Foreach ($Entry in $ImportedCsv)
Switch ($Entry."order")
1 $Entry."order" = "0"

$Entry

$NewCSV | Export-CSV done.csv -Delimiter '|' -NoTypeInformation

(Get-Content -Path done.csv ).Replace('"','') | Set-Content -Path done.csv


The result is this:



order|color|shape
0|green|square
0|red|rectangle


My data.csv wont have headers to start with.


Can "Switch ($Entry."order")" use column numbers rather than the header name?

So, something like "Column 1" rather then "order"?



Please no mention of simply adding the headers then delete the headers when done.

In the end, i need to change a column's data based on column number, rather than header name.


So...replace ($Entry."order") with what?


($Entry.Column 1) = nope

($Entry.Column-1) = nope


Thanks for any help.







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:00

























asked Nov 9 at 22:33









Mobs

106




106







  • 1




    the Import-CSV cmdlet has a -Header parameter that will allow you to set your header line when the file itself has none. take a look at Get-Help Import-CSV for more info. [grin]
    – Lee_Dailey
    Nov 9 at 22:54












  • 1




    the Import-CSV cmdlet has a -Header parameter that will allow you to set your header line when the file itself has none. take a look at Get-Help Import-CSV for more info. [grin]
    – Lee_Dailey
    Nov 9 at 22:54







1




1




the Import-CSV cmdlet has a -Header parameter that will allow you to set your header line when the file itself has none. take a look at Get-Help Import-CSV for more info. [grin]
– Lee_Dailey
Nov 9 at 22:54




the Import-CSV cmdlet has a -Header parameter that will allow you to set your header line when the file itself has none. take a look at Get-Help Import-CSV for more info. [grin]
– Lee_Dailey
Nov 9 at 22:54












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Let's assume you have:



data_noheader.csv





7|green|square
8|red|rectangle
9|blue|triangle


And you want to change the green square (in the first row) to purple and the item where col0 is 8 to gold star.



A few things change:



  1. Explicitly set the header with -Header so that the first line isn't treated as the header.

  2. Edit $NewCSV in place rather than composing it with ForEach (this isn't required but makes the code a bit easier to follow and work with)

  3. Instead of looping ForEach and looking at the column name the row is selected explicitly for things that you want to change by the actual row.

  4. For things that you want to change based on the value of a column use a similar pattern to the one you used before and just check the value.

  5. Strip the header back off with Select-Object -Skip 1

$NewCSV = Import-CSV data_noheader.csv -Delimiter '|' -Header @("col0", "col1", "col2")
$NewCSV[0].col1 = "purple" # Set based on the actual row position
ForEach ($Entry in $NewCSV)
If ($Entry.col0 -Eq "8") # Modify data in other columns in a row based on the value of a particular column
$Entry.col1 = "gold"
$Entry.col2 = "star"


$NewCSV | Export-CSV done_noheader.csv -Delimiter '|' -NoTypeInformation

(Get-Content -Path done_noheader.csv ).Replace('"','') | Select-Object -Skip 1 | Set-Content -Path done_noheader.csv


The result in done_noheader.csv will now be:





7|purple|square
8|gold|star
9|blue|triangle





share|improve this answer






















  • Thx, this explains more than i really need. What i need is to change the first column to all zeros. Your second line does the first row. This, $NewCSV[0].col1 = "0" will do the first row. What do i put here, [?], for all rows? Sry, I'm a novice.
    – Mobs
    Nov 11 at 23:29










  • To do it for all rows you'll want to use a for or foreach loop
    – Tyler Szabo
    Nov 11 at 23:33










  • Yep, thx... Just worked that out. Thanks for the help. Two thumbs up!
    – Mobs
    Nov 11 at 23:47

















up vote
0
down vote













In case the delimiter is known and unique (not part of quoted column data)



You can obtain column count



$delim = '|'
$Cols = (get-Content .data.csv|select -first 1).split($delim).Count


And automatically apply numbered Header



$csv = Import-Csv .data.csv -Delim $delim -Header @(1..$Cols)



> $csv

1 2 3
- - -
7 purple square
8 gold star
9 blue triangle


Manipulate the columns in a ForEach



$csv | ForEach-Object $_.1 = 0 



> $csv

1 2 3
- - -
0 purple square
0 gold star
0 blue triangle


And lateron save, stripping quotes and header if neccessary.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234122%2fpowershell-change-data-in-a-csvs-column-that-has-no-headers%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Let's assume you have:



    data_noheader.csv





    7|green|square
    8|red|rectangle
    9|blue|triangle


    And you want to change the green square (in the first row) to purple and the item where col0 is 8 to gold star.



    A few things change:



    1. Explicitly set the header with -Header so that the first line isn't treated as the header.

    2. Edit $NewCSV in place rather than composing it with ForEach (this isn't required but makes the code a bit easier to follow and work with)

    3. Instead of looping ForEach and looking at the column name the row is selected explicitly for things that you want to change by the actual row.

    4. For things that you want to change based on the value of a column use a similar pattern to the one you used before and just check the value.

    5. Strip the header back off with Select-Object -Skip 1

    $NewCSV = Import-CSV data_noheader.csv -Delimiter '|' -Header @("col0", "col1", "col2")
    $NewCSV[0].col1 = "purple" # Set based on the actual row position
    ForEach ($Entry in $NewCSV)
    If ($Entry.col0 -Eq "8") # Modify data in other columns in a row based on the value of a particular column
    $Entry.col1 = "gold"
    $Entry.col2 = "star"


    $NewCSV | Export-CSV done_noheader.csv -Delimiter '|' -NoTypeInformation

    (Get-Content -Path done_noheader.csv ).Replace('"','') | Select-Object -Skip 1 | Set-Content -Path done_noheader.csv


    The result in done_noheader.csv will now be:





    7|purple|square
    8|gold|star
    9|blue|triangle





    share|improve this answer






















    • Thx, this explains more than i really need. What i need is to change the first column to all zeros. Your second line does the first row. This, $NewCSV[0].col1 = "0" will do the first row. What do i put here, [?], for all rows? Sry, I'm a novice.
      – Mobs
      Nov 11 at 23:29










    • To do it for all rows you'll want to use a for or foreach loop
      – Tyler Szabo
      Nov 11 at 23:33










    • Yep, thx... Just worked that out. Thanks for the help. Two thumbs up!
      – Mobs
      Nov 11 at 23:47














    up vote
    1
    down vote













    Let's assume you have:



    data_noheader.csv





    7|green|square
    8|red|rectangle
    9|blue|triangle


    And you want to change the green square (in the first row) to purple and the item where col0 is 8 to gold star.



    A few things change:



    1. Explicitly set the header with -Header so that the first line isn't treated as the header.

    2. Edit $NewCSV in place rather than composing it with ForEach (this isn't required but makes the code a bit easier to follow and work with)

    3. Instead of looping ForEach and looking at the column name the row is selected explicitly for things that you want to change by the actual row.

    4. For things that you want to change based on the value of a column use a similar pattern to the one you used before and just check the value.

    5. Strip the header back off with Select-Object -Skip 1

    $NewCSV = Import-CSV data_noheader.csv -Delimiter '|' -Header @("col0", "col1", "col2")
    $NewCSV[0].col1 = "purple" # Set based on the actual row position
    ForEach ($Entry in $NewCSV)
    If ($Entry.col0 -Eq "8") # Modify data in other columns in a row based on the value of a particular column
    $Entry.col1 = "gold"
    $Entry.col2 = "star"


    $NewCSV | Export-CSV done_noheader.csv -Delimiter '|' -NoTypeInformation

    (Get-Content -Path done_noheader.csv ).Replace('"','') | Select-Object -Skip 1 | Set-Content -Path done_noheader.csv


    The result in done_noheader.csv will now be:





    7|purple|square
    8|gold|star
    9|blue|triangle





    share|improve this answer






















    • Thx, this explains more than i really need. What i need is to change the first column to all zeros. Your second line does the first row. This, $NewCSV[0].col1 = "0" will do the first row. What do i put here, [?], for all rows? Sry, I'm a novice.
      – Mobs
      Nov 11 at 23:29










    • To do it for all rows you'll want to use a for or foreach loop
      – Tyler Szabo
      Nov 11 at 23:33










    • Yep, thx... Just worked that out. Thanks for the help. Two thumbs up!
      – Mobs
      Nov 11 at 23:47












    up vote
    1
    down vote










    up vote
    1
    down vote









    Let's assume you have:



    data_noheader.csv





    7|green|square
    8|red|rectangle
    9|blue|triangle


    And you want to change the green square (in the first row) to purple and the item where col0 is 8 to gold star.



    A few things change:



    1. Explicitly set the header with -Header so that the first line isn't treated as the header.

    2. Edit $NewCSV in place rather than composing it with ForEach (this isn't required but makes the code a bit easier to follow and work with)

    3. Instead of looping ForEach and looking at the column name the row is selected explicitly for things that you want to change by the actual row.

    4. For things that you want to change based on the value of a column use a similar pattern to the one you used before and just check the value.

    5. Strip the header back off with Select-Object -Skip 1

    $NewCSV = Import-CSV data_noheader.csv -Delimiter '|' -Header @("col0", "col1", "col2")
    $NewCSV[0].col1 = "purple" # Set based on the actual row position
    ForEach ($Entry in $NewCSV)
    If ($Entry.col0 -Eq "8") # Modify data in other columns in a row based on the value of a particular column
    $Entry.col1 = "gold"
    $Entry.col2 = "star"


    $NewCSV | Export-CSV done_noheader.csv -Delimiter '|' -NoTypeInformation

    (Get-Content -Path done_noheader.csv ).Replace('"','') | Select-Object -Skip 1 | Set-Content -Path done_noheader.csv


    The result in done_noheader.csv will now be:





    7|purple|square
    8|gold|star
    9|blue|triangle





    share|improve this answer














    Let's assume you have:



    data_noheader.csv





    7|green|square
    8|red|rectangle
    9|blue|triangle


    And you want to change the green square (in the first row) to purple and the item where col0 is 8 to gold star.



    A few things change:



    1. Explicitly set the header with -Header so that the first line isn't treated as the header.

    2. Edit $NewCSV in place rather than composing it with ForEach (this isn't required but makes the code a bit easier to follow and work with)

    3. Instead of looping ForEach and looking at the column name the row is selected explicitly for things that you want to change by the actual row.

    4. For things that you want to change based on the value of a column use a similar pattern to the one you used before and just check the value.

    5. Strip the header back off with Select-Object -Skip 1

    $NewCSV = Import-CSV data_noheader.csv -Delimiter '|' -Header @("col0", "col1", "col2")
    $NewCSV[0].col1 = "purple" # Set based on the actual row position
    ForEach ($Entry in $NewCSV)
    If ($Entry.col0 -Eq "8") # Modify data in other columns in a row based on the value of a particular column
    $Entry.col1 = "gold"
    $Entry.col2 = "star"


    $NewCSV | Export-CSV done_noheader.csv -Delimiter '|' -NoTypeInformation

    (Get-Content -Path done_noheader.csv ).Replace('"','') | Select-Object -Skip 1 | Set-Content -Path done_noheader.csv


    The result in done_noheader.csv will now be:





    7|purple|square
    8|gold|star
    9|blue|triangle






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 23:22

























    answered Nov 9 at 23:13









    Tyler Szabo

    5091518




    5091518











    • Thx, this explains more than i really need. What i need is to change the first column to all zeros. Your second line does the first row. This, $NewCSV[0].col1 = "0" will do the first row. What do i put here, [?], for all rows? Sry, I'm a novice.
      – Mobs
      Nov 11 at 23:29










    • To do it for all rows you'll want to use a for or foreach loop
      – Tyler Szabo
      Nov 11 at 23:33










    • Yep, thx... Just worked that out. Thanks for the help. Two thumbs up!
      – Mobs
      Nov 11 at 23:47
















    • Thx, this explains more than i really need. What i need is to change the first column to all zeros. Your second line does the first row. This, $NewCSV[0].col1 = "0" will do the first row. What do i put here, [?], for all rows? Sry, I'm a novice.
      – Mobs
      Nov 11 at 23:29










    • To do it for all rows you'll want to use a for or foreach loop
      – Tyler Szabo
      Nov 11 at 23:33










    • Yep, thx... Just worked that out. Thanks for the help. Two thumbs up!
      – Mobs
      Nov 11 at 23:47















    Thx, this explains more than i really need. What i need is to change the first column to all zeros. Your second line does the first row. This, $NewCSV[0].col1 = "0" will do the first row. What do i put here, [?], for all rows? Sry, I'm a novice.
    – Mobs
    Nov 11 at 23:29




    Thx, this explains more than i really need. What i need is to change the first column to all zeros. Your second line does the first row. This, $NewCSV[0].col1 = "0" will do the first row. What do i put here, [?], for all rows? Sry, I'm a novice.
    – Mobs
    Nov 11 at 23:29












    To do it for all rows you'll want to use a for or foreach loop
    – Tyler Szabo
    Nov 11 at 23:33




    To do it for all rows you'll want to use a for or foreach loop
    – Tyler Szabo
    Nov 11 at 23:33












    Yep, thx... Just worked that out. Thanks for the help. Two thumbs up!
    – Mobs
    Nov 11 at 23:47




    Yep, thx... Just worked that out. Thanks for the help. Two thumbs up!
    – Mobs
    Nov 11 at 23:47












    up vote
    0
    down vote













    In case the delimiter is known and unique (not part of quoted column data)



    You can obtain column count



    $delim = '|'
    $Cols = (get-Content .data.csv|select -first 1).split($delim).Count


    And automatically apply numbered Header



    $csv = Import-Csv .data.csv -Delim $delim -Header @(1..$Cols)



    > $csv

    1 2 3
    - - -
    7 purple square
    8 gold star
    9 blue triangle


    Manipulate the columns in a ForEach



    $csv | ForEach-Object $_.1 = 0 



    > $csv

    1 2 3
    - - -
    0 purple square
    0 gold star
    0 blue triangle


    And lateron save, stripping quotes and header if neccessary.






    share|improve this answer
























      up vote
      0
      down vote













      In case the delimiter is known and unique (not part of quoted column data)



      You can obtain column count



      $delim = '|'
      $Cols = (get-Content .data.csv|select -first 1).split($delim).Count


      And automatically apply numbered Header



      $csv = Import-Csv .data.csv -Delim $delim -Header @(1..$Cols)



      > $csv

      1 2 3
      - - -
      7 purple square
      8 gold star
      9 blue triangle


      Manipulate the columns in a ForEach



      $csv | ForEach-Object $_.1 = 0 



      > $csv

      1 2 3
      - - -
      0 purple square
      0 gold star
      0 blue triangle


      And lateron save, stripping quotes and header if neccessary.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        In case the delimiter is known and unique (not part of quoted column data)



        You can obtain column count



        $delim = '|'
        $Cols = (get-Content .data.csv|select -first 1).split($delim).Count


        And automatically apply numbered Header



        $csv = Import-Csv .data.csv -Delim $delim -Header @(1..$Cols)



        > $csv

        1 2 3
        - - -
        7 purple square
        8 gold star
        9 blue triangle


        Manipulate the columns in a ForEach



        $csv | ForEach-Object $_.1 = 0 



        > $csv

        1 2 3
        - - -
        0 purple square
        0 gold star
        0 blue triangle


        And lateron save, stripping quotes and header if neccessary.






        share|improve this answer












        In case the delimiter is known and unique (not part of quoted column data)



        You can obtain column count



        $delim = '|'
        $Cols = (get-Content .data.csv|select -first 1).split($delim).Count


        And automatically apply numbered Header



        $csv = Import-Csv .data.csv -Delim $delim -Header @(1..$Cols)



        > $csv

        1 2 3
        - - -
        7 purple square
        8 gold star
        9 blue triangle


        Manipulate the columns in a ForEach



        $csv | ForEach-Object $_.1 = 0 



        > $csv

        1 2 3
        - - -
        0 purple square
        0 gold star
        0 blue triangle


        And lateron save, stripping quotes and header if neccessary.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 23:55









        LotPings

        16k61531




        16k61531



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234122%2fpowershell-change-data-in-a-csvs-column-that-has-no-headers%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ruanda

            Makov (Slowakei)

            Kleinkühnau