Is there a PowerShell equivalent of `paste` (i.e., horizontal file concatenation)? [duplicate]
Asked Answered
J

1

0

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?

Jewelry answered 21/6, 2021 at 14:59 Comment(2)
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.Abernethy
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.Brood
G
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)

Glamour answered 21/6, 2021 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.