1

I want to horizontally concatenate a bunch of CSV files using PowerShell. (When considering possible "duplicate" questions", please not that "a bunch" is not "two".) What is the PowerShell equivalent of the Linux paste command?

2
  • 1
    There is no equivalent command. You'd want to import both files with Import-Csv, enumerate through them to build a new object for each record, and then write the result with Export-Csv. You could do the same with Get-Content, I suppose, but if you're going to bother with doing it yourself I can't imagine not wanting to benefit from the CSV parser in the general case. Commented Jun 21, 2021 at 15:06
  • As Bacon Bits notes, there's nothing built in. For files (only), direct use of .NET APIs offers a solution; see this answer to the linked duplicate. iRon's proposal would certainly make for a nice addition to PowerShell. Commented Jun 21, 2021 at 19:00

1 Answer 1

3

A few months ago, I submitted a proposal for including a Join-Object cmdlet to the standard PowerShell equipment #14994.

Besides complexer joins based on a related property, the idea is to also be able to do a side-by-side join (by omiting the -On parameter).
Taken this Paste command in Linux as an example:

$State =
'Arunachal Pradesh',
'Assam',
'Andhra Pradesh',
'Bihar',
'Chhattisgrah'

$Capital =
'Itanagar',
'Dispur',
'Hyderabad',
'Patna',
'Raipur'

Installation

Install-Module -Name JoinModule

Creating a side-by-side array:

1..5 |Join $State |Join $Capital |% { "$_" }

1 Arunachal Pradesh Itanagar
2 Assam Dispur
3 Andhra Pradesh Hyderabad
4 Bihar Patna
5 Chhattisgrah Raipur

Creating a side-by-side object:

1..5 |Join $State |Join $Capital -Name Number, State, Capital

Number State             Capital
------ -----             -------
     1 Arunachal Pradesh Itanagar
     2 Assam             Dispur
     3 Andhra Pradesh    Hyderabad
     4 Bihar             Patna
     5 Chhattisgrah      Raipur

Concatenate ("Paste") two objects:

$List = $State |Join $Capital -Name State, Capital
$Id = ConvertFrom-Csv @'
Id
A
B
C
D
E
'@

$Id |Join $List

Id State             Capital
-- -----             -------
A  Arunachal Pradesh Itanagar
B  Assam             Dispur
C  Andhra Pradesh    Hyderabad
D  Bihar             Patna
E  Chhattisgrah      Raipur

References:

Please give a 👍 if you support the proposal to Add a Join-Object cmdlet to the standard PowerShell equipment (#14994)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.