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.
powershell
add a comment |
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.
powershell
1
theImport-CSVcmdlet has a-Headerparameter that will allow you to set your header line when the file itself has none. take a look atGet-Help Import-CSVfor more info. [grin]
– Lee_Dailey
Nov 9 at 22:54
add a comment |
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.
powershell
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
powershell
edited Nov 11 at 18:00
asked Nov 9 at 22:33
Mobs
106
106
1
theImport-CSVcmdlet has a-Headerparameter that will allow you to set your header line when the file itself has none. take a look atGet-Help Import-CSVfor more info. [grin]
– Lee_Dailey
Nov 9 at 22:54
add a comment |
1
theImport-CSVcmdlet has a-Headerparameter that will allow you to set your header line when the file itself has none. take a look atGet-Help Import-CSVfor 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
add a comment |
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:
- Explicitly set the header with
-Headerso that the first line isn't treated as the header. - Edit
$NewCSVin place rather than composing it withForEach(this isn't required but makes the code a bit easier to follow and work with) - Instead of looping
ForEachand looking at the column name the row is selected explicitly for things that you want to change by the actual row. - 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.
- 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
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
add a comment |
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.
add a comment |
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:
- Explicitly set the header with
-Headerso that the first line isn't treated as the header. - Edit
$NewCSVin place rather than composing it withForEach(this isn't required but makes the code a bit easier to follow and work with) - Instead of looping
ForEachand looking at the column name the row is selected explicitly for things that you want to change by the actual row. - 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.
- 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
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
add a comment |
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:
- Explicitly set the header with
-Headerso that the first line isn't treated as the header. - Edit
$NewCSVin place rather than composing it withForEach(this isn't required but makes the code a bit easier to follow and work with) - Instead of looping
ForEachand looking at the column name the row is selected explicitly for things that you want to change by the actual row. - 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.
- 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
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
add a comment |
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:
- Explicitly set the header with
-Headerso that the first line isn't treated as the header. - Edit
$NewCSVin place rather than composing it withForEach(this isn't required but makes the code a bit easier to follow and work with) - Instead of looping
ForEachand looking at the column name the row is selected explicitly for things that you want to change by the actual row. - 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.
- 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
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:
- Explicitly set the header with
-Headerso that the first line isn't treated as the header. - Edit
$NewCSVin place rather than composing it withForEach(this isn't required but makes the code a bit easier to follow and work with) - Instead of looping
ForEachand looking at the column name the row is selected explicitly for things that you want to change by the actual row. - 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.
- 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
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 9 at 23:55
LotPings
16k61531
16k61531
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
the
Import-CSVcmdlet has a-Headerparameter that will allow you to set your header line when the file itself has none. take a look atGet-Help Import-CSVfor more info. [grin]– Lee_Dailey
Nov 9 at 22:54