Join Header column

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 1 year, 9 months ago.

  • Author
    Posts
  • #27086
    Profile photo of PowerShell84
    PowerShell84
    Participant

    Hi powerShell,

    I am written script in powershell:

    $input = @(Get-Content H:\bb.txt)
    $bb = @(foreach($line in $input){
    $line
    })

    Get-Content H:\aa.txt | ConvertFrom-Csv -Header (1..2)|
    ForEach-Object { $_.'2' = $bb ; $_ } |
    ConvertTo-Csv -NoTypeInformation |
    Select-Object -Skip 1 |
    ForEach-Object { $_ -replace '"'," }

    Where aa.txt:
    aa
    aa
    aa
    aa

    Where bb.txt:
    bb
    bb
    bb
    bb

    output:
    aa,System.Object[]
    aa,System.Object[]
    aa,System.Object[]
    aa,System.Object[]

    The display output should like this as below:
    aa,bb
    aa,bb
    aa,bb
    aa,bb

    Any idea?
    Thanks

  • #27087
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    here ForEach-Object { $_.'2' = $bb ; $_ } |
    you assign array $bb to column 2 of every line from $aa
    and after converting it to csv you get array csv representation as `System.Object[]`
    if you need to combine every line of aa.txt with every line of bb.txt
    you should at least assign it as $_.'2' = $bb[$index] where $index is line number.
    may be you should convert your cycle to
    for ($index=0; $index -lt $bb.count; $index++){
    $aa[$index].'2' = $bb[$index]
    }
    form

  • #27094
    Profile photo of PowerShell84
    PowerShell84
    Participant

    what is $aa? I do not get it. I tired this one but error
    $input = @(Get-Content H:\bb.txt)
    $bb = foreach($line in $input){
    }

    Get-Content H:\aa.txt | ConvertFrom-Csv -Header (1..2)|
    #ForEach-Object { $_.'2' = $test ; $_ } |
    @(for($i=0; $i -lt $bb.count; $i++){ $aa[$i].'2' = $bb[$i]}form)
    ConvertTo-Csv -NoTypeInformation |
    Select-Object -Skip 1 |
    ForEach-Object { $_ -replace '"'," }

  • #27096
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    I can write all script for you but this way you never learn ::)

    $aa is the content of aa.txt as well as $bb is content of bb.txt

    btw,
    $input = @(Get-Content H:\bb.txt)
    $bb = @(foreach($line in $input){
    $line
    })

    is equal to
    $bb=Get-Content H:\bb.txt

You must be logged in to reply to this topic.