Author Posts

July 2, 2015 at 10:49 pm

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

July 2, 2015 at 11:44 pm

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

July 3, 2015 at 1:27 am

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 '"'," }

July 3, 2015 at 4:23 am

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