Merge two columns in CSV file from command line / PowerShell - 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

Related

import and export csv file in powershell

i have a csv file "emp.csv" with following information.
empid status
1 t
2 a
3 t
4 t
5 a
I need empid and status of employees having status="t" and the output should be in "res.csv" file
For this, you'll first want to import the existing data with Import-Csv - but since your file is technically a TSV (tab-separated instead of comma-separated), make sure you specify this with -Delimiter "`t":
$employeeStatusData = Import-Csv .\path\to\emp.csv -Delimiter "`t"
Import-Csv will parse the input file and store the resulting objects in $employeeStatusData. To filter these object based on the value of the status property/column, use Where-Object:
$filteredEmployeeData = $employeeStatusData |Where-Object status -eq 't'
Now that you have the relevant rows selected, you can export the data again with Export-Csv:
$filteredEmployeeData |Export-Csv .\path\to\res.csv -Delimiter "`t" -NoTypeInformation

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

How do I merge 2 adjacent columns in a CSV file into a single column, separated by comma? (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"

How to export data into a specific column in a csv file in PowerShell

What I am trying to do is I import data from a csv file which has UserPrincipalnames and I am taking the names before the # symbol and then I want to export that data to a specific column in the same CSV file which in this case is o365Users.csv. I am able to write it out to a text file but I need to know how to export it out to Column G with the header name as SAM
This is my code:
$Addys = Import-Csv "C:\scripts\o365Users.csv"
$UPNs = $Addys.UserPrincipalName
foreach ($UPN in $UPNs) {
$Name = $UPN.Split("#")[0]
Write-Output $Name >> c:\scripts\o365Names.txt
}
To append a new column with the header SAM use Select-Object with a calculated property:
(Import-Csv 'C:\scripts\o365Users.csv') |
Select-Object -Property *,#{n='SAM';e={$_.UserPrincipalName.Split('#')[0]}}
If the new property has to be in a specific position you can't use the wildcard * but will have to enumerate all headers/columns/properties in the desired order, i.e.
(Import-Csv 'C:\scripts\o365Users.csv') |
Select-Object -Property ColA,ColB,ColC,ColD,ColE,ColF,#{n='SAM';e={$_.UserPrincipalName.Split('#')[0]}},ColH
replace Col_ with your real headers.
Due to enclosing the (Import-Csv) in parentheses you can export to the same file name (not recommended while still testing) - simply append
| Export-Csv 'C:\scripts\o365Users.csv' -NoTypeInformation
Here is a quick way to get just the output you are looking for. You would import the current CSV. Create an blank output array and in your loop add each name. Then export the CSV
$Addys = Import-Csv "C:\scripts\o365Users.csv"
$UPNs = $Addys.UserPrincipalName
[System.Collections.ArrayList]$Output = #()
foreach ($UPN in $UPNs) {
$Name = $UPN.Split("#")[0]
$Output.Add($Name) | Out-Null
}
$Output | Export-Csv -Path "C:\scripts\o365Users.csv" -NoTypeInformation

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.