How do I merge 2 adjacent columns in a CSV file into a single column, separated by comma? (PowerShell) - powershell

I have a CSV file with 2 columns, latitude & longitude. I am trying to merge the 2 columns into 1, separated by a comma (no spaces).
Input CSV file, first 5 rows
latitude longitude
35.1868 -106.6652
42.3688 -83.4799
40.3926 -79.9052
40.5124 -88.9883
38.5352 -90.0006
My goal is to take this CSV and create a new one with a single column with both values separated by a comma (no spaces in-between) using PowerShell. See the desired output below...
location
35.1868,-106.6652
42.3688,-83.4799
40.3926,-79.9052
40.5124,-88.9883
38.5352,-90.0006
Any help will be greatly appreciated!

The IMO easiest way is a Select-Object with a calculated property
Import-Csv .\input.csv |
Select-Object #{Name='Location';Expression={$_.latitude,$_.longitude -join ','}} |
Export-Csv .\output.csv -NoTypeInformation
> Get-Content .\output.csv
"Location"
"35.1868,-106.6652"
"42.3688,-83.4799"
"40.3926,-79.9052"
"40.5124,-88.9883"
"38.5352,-90.0006"
Edit
In case there are other columns which should not be affected by the merge,
see this modified Select-Object
Select-Object *,#{N='Location';E={$_.latitude,$_.longitude -join ','}} -Exclude latitude,longitude|
But the new column will then be the last one.

the 1st ten lines are just a way to embed sample data in a script without needing to write it to a file & then read it back in. [grin]
use Import-CSV to get the real data into the script.
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = #'
latitude, longitude
35.1868, -106.6652
42.3688, -83.4799
40.3926, -79.9052
40.5124, -88.9883
38.5352, -90.0006
'# | ConvertFrom-Csv
$LocationList = foreach ($IS_Item in $InStuff)
{
[PSCustomObject]#{
Location = #($IS_Item.Latitude, $IS_Item.Longitude) -join ','
}
}
# on screen
$LocationList
# CSV file
$LocationList |
Export-Csv -LiteralPath "$env:TEMP\JohnnyCarino_LocationList.csv" -NoTypeInformation
screen output ...
Location
--------
35.1868,-106.6652
42.3688,-83.4799
40.3926,-79.9052
40.5124,-88.9883
38.5352,-90.0006
CSV file content ...
"Location"
"35.1868,-106.6652"
"42.3688,-83.4799"
"40.3926,-79.9052"
"40.5124,-88.9883"
"38.5352,-90.0006"

Related

Using Powershell, how can I export and delete csv rows, where a particular value is *not found* in a *different* csv?

I have two files. One is called allper.csv
institutiongroup,studentid,iscomplete
institutionId=22343,123,FALSE
institutionId=22343,456,FALSE
institutionId=22343,789,FALSE
The other one is called actswithpersons.csv
abc,123;456
def,456
ghi,123
jkl,123;456
Note: The actswithpersons.csv does not have headers - they are going to be added in later via an excel power query so don't want them in there now. The actswithpersons csv columns are delimited with commas - there are only two columns, and the second one contains multiple personids - again Excel will deal with this later.
I want to remove all rows from allper.csv where the personid doesn't appear in actswithpersons.csv, and export them to another csv. So in the desired outcome, allper.csv would look like this
institutiongroup,studentid,iscomplete
institutionId=22343,123,FALSE
institutionId=22343,456,FALSE
and the export.csv would look like this
institutiongroup,studentid,iscomplete
institutionId=22343,789,FALSE
I've got as far as the below, which will put into the shell whether the personid is found in the actswithpersons.csv file.
$donestuff = (Get-Content .\ActsWithpersons.csv | ConvertFrom-Csv); $ids=(Import-Csv .\allper.csv);foreach($id in $ids.personid) {echo $id;if($donestuff -like "*$id*" )
{
echo 'Contains String'
}
else
{
echo 'Does not contain String'
}}
However, I'm not sure how to go the last step, and export & remove the unwanted rows from allper.csv
I've tried (among many things)
$donestuff = (Get-Content .\ActsWithpersons.csv | ConvertFrom-Csv);
Import-Csv .\allper.csv |
Where-Object {$donestuff -notlike $_.personid} |
Export-Csv -Path export.csv -NoTypeInformation
This took a really long time and left me with an empty csv. So, if you can give any guidance, please help.
Since your actswithpersons.csv doesn't have headers, in order for you to import as csv, you can specify the -Header parameter in either Import-Csv or ConvertFrom-Csv; with the former cmdlet being the better solution.
With that said, you can use any header name for those 2 columns then filter by the given column name (ID in this case) after your import of allper.csv using Where-Object:
$awp = (Import-Csv -Path '.\actswithpersons.csv' -Header 'blah','ID').ID.Split(';')
Import-Csv -Path '.\allper.csv' | Where-Object -Property 'Studentid' -notin $awp
This should give you:
institutiongroup studentid iscomplete
---------------- --------- ----------
institutionId=22343 789 FALSE
If you're looking to do it with Get-Content you can split by the delimiters of , and ;. This should give you just a single row of values which you can then compare the entirety of variable ($awp) using the same filter as above which will give you the same results:
$awp = (Get-Content -Path '.\actswithpersons.csv') -split ",|;"
Import-Csv -Path '.\allper.csv' | Where-Object -Property 'Studentid' -notin $awp

Merge two columns in CSV file from command line / PowerShell

I have a CSV file where either column "EAN" (col. 9) or column "UPC" (col. 10) is set (some lines with both). What I need is a CSV where both are combined into one column, something like
if EAN <> "" use EAN else use UPC
to be put into a batch/PowerShell file. Additionally I need to keep col. 11, everything else can be discarded. The output file only has to contain two columns, either EAN or UPC plus stock level. Any help would be appreciated.
The original CSV is semicolon seperated with possible empty values, but no quoted values with the seperator or newline characters.
Example input:
company;dept;sku;desc;secsku;unit;size;year;ean;upc;stock;exact(CRLF)
Stack;Overflow;000-000;Question0;00;pcs;XL;2021;1111111111111;;1;6(CRLF)
Overflow;Stack;000-001;Question1;01;pcs;L;2021;;222222222222;1;9(CRLF)
The output should look like:
ean;stock(CRLF)
1111111111111;1(CRLF)
222222222222;1(CRLF)
One way is to use a calculated property:
# import the source csv file
Import-Csv -Path 'D:\Test\test.csv' -Delimiter ';' |
# select only two of its properties where 'ean' is a calculated property
Select-Object #{Name = 'ean'; Expression = {if(![string]::IsNullOrWhiteSpace($_.ean)) {$_.ean} else {$_.upc}}}, stock |
# output to a new CSV file
Export-Csv -Path 'D:\Test\updated.csv' -Delimiter ';' -NoTypeInformation
Another way of doing that (less concise) is by using a PsCustomObject
# import the source csv file
Import-Csv -Path 'D:\Test\test.csv' -Delimiter ';' |
# loop through the data
ForEach-Object {
# output an object with two properties
[PsCustomObject]#{
ean = if(![string]::IsNullOrWhiteSpace($_.ean)) { $_.ean } else { $_.upc }
stock = $_.stock
}
} |
# output to a new CSV file
Export-Csv -Path 'D:\Test\updated.csv' -Delimiter ';' -NoTypeInformation

Updating Active directory DN in csv records with Powershell

I'm completely stuck on how to update multiple rows in a CSV file with new values.. the issue is as follows, I import an active directory csv export file into my powershell which has roughly 500 users, like so:
$Users = Import-CSV "C:\Users\administrator\userExport.csv"
I then need to update the DN column so that they have the new active directory structure on the new domain
Current structure:
DN:
-----------------
CN=John Smith,OU=Users,DC=XTR,DC=ORG
Required new domain structure
DN:
-----------------
CN=John Smith,OU=Users,OU=Administration,DC=RTS,DC=LIVE,DC=LOCAL
The trouble is I have no idea how I can sequentially go through the entries in this CSV files to update only the necessary elements of the entry via powershell, any help with this would be much appreciated.
Sample line from csv file:
DN objectClass instanceType
-- ------------ ------------
CN=John Smith,OU=Users,DC=XTR,DC=ORG user 4
Apologies, I know how vague the request is but I'm sure there must be an elegant way of doing this but my powershell knowledge is extremely limited.
Thanks for your time.
Import-Csv will convert each row in the CSV to an object with properties corresponding to the column headers and the column value of the current row. This makes them easy to manipulate:
$Users = Import-CSV "C:\Users\administrator\userExport.csv"
$Users |ForEach-Object {
# Update DN column value
$_.DN = $_.DN -replace ',DC=XTR,DC=ORG$', ',OU=Administration,DC=RTS,DC=LIVE,DC=LOCAL'
# Output modified object
$_
} |Export-Csv C:\Users\administrator\userExportModified.csv -NoTypeInformation
You should put your CSV sample "as is", and not trying to format it. How can we know if the CSV is well-formed ?
I am trying to answer anyway, based on a well-formed CSV, with default delimiter (comma) :
Import-CSV "C:\Users\administrator\sourceUsers.csv" | ForEach {
$_.DN = $_.DN -replace "OU=Users,DC=XTR,DC=ORG","OU=Users,OU=Administration,DC=RTS,DC=LIVE,DC=LOCAL"
# ^
# Are you sure it is actually 'OU' here and not 'CN'
$_
} | Export-Csv "C:\Users\administrator\destinationUsers.csv" -NoTypeInformation -Enconding UTF8
If there is another delimiter, eg semi colon :
Import-CSV "C:\Users\administrator\sourceUsers.csv" -Delimiter ';' | ForEach {
$_.DN = $_.DN -replace "OU=Users,DC=XTR,DC=ORG","OU=Users,OU=Administration,DC=RTS,DC=LIVE,DC=LOCAL"
# ^
# Are you sure it is actually 'OU' here and not 'CN'
$_
} | Export-Csv "C:\Users\administrator\destinationUsers.csv" -NoTypeInformation -Enconding UTF8
Edit :
Mathias answered during I was typing my own one :), I keep it only because of my remarks about CSV and the 'OU=Users...'

Need 3 digits in column in a CSV-file

I have a script that extracts data from an XML file and put this into an CSV file with 2 columns.
The file looks like this:
Name, Count
1,34
3,55
15,66
103,99
etc.
So far so good...
My problem is that the program that reads the CSV-file always expect 3 digits in the column "Name".
So the CSV-file need to look like this:
Name, Count
001,34
003,55
015,66
103,99
etc.
How can I do this formatting using "Export-CSV"?
Please help I'm stuck here..
There are several ways to apply the changes to the csv file.
Read/Import to a variable, change name, Export-Csv variable
$Csv = Import-Csv .\Sample.csv
$Csv | ForEach-Object{ $_.Name = $_.Name.PadLeft(3,'0') }
$Csv | Export-Csv .\NewSample.csv -NoTypeInformation
Do the same on the fly with a calculated property reusing the same header/property name.
Import-Csv .\Sample.csv |
Select-Object #{n='Name';e={$_.Name.PadLeft(3,'0')}},Count|
Export-Csv .\NewSample2.csv -NoTypeInformation
Use the -f ( format ) operator with variable
i.e.
[int] $int = 25;
"{0:D3}" -f $int
Here 3 is a number of digits and Output will be :
025

Manipulate CSV files with Powershell (generate column for hashkey)

I have a CSV file with about 10 columns separated with a ; (semicolon). I would like to add another column which generates a hashkey for the first columns value.
Is there a possibility in Powershell to do this? Also are there short haskeys (up to 10 to 15 chars)?
Example:
Old:
10000;value2;value3....
New:
HashkeyOf10000;1000;value2;value3...
You can use a calculated property for adding a column to a CSV:
$csv = 'C:\path\to\your.csv'
(Import-Csv $csv -Delimiter ';') |
select -Property #{n='Hashkey';e={Calc-Hash $_.A}},* |
Export-Csv $csv -Delimiter ';' -NoType
Replace Calc-Hash with the actual name of your hash function and A with the actual name of the first column of your CSV.
The parentheses around Import-Csv are required to ensure that reading the file is completed before writing the output starts.