Transforming Rows into Columns from awk to Powershell

Hello Guru's,

EDIT: I do have an awk script that does most of the job for me. An equivalent to this awk script would be just fine. 

awk '{t=$1;gsub(/"/,"",t);a[t]=a[t] FS $0} END {for (i in a) print a[i]}' filename

Making sure I post under the right category...windows powershell!

I have an awk script that does the job somewhat but I need a powershell script that transforms rows into columns.

Basically I need to join the rows into columns if the first column has the same value. 

Below is the sample input file.
A 1,2,3
A 4,5,6
B 1,2,3
B 4,5,6
C 1,2,3
C 2,3,4
C 3,4,5

Here is the desired output: 
A 1,2,3,4,5,6
B 1,2,3,4,5,6
C 1,2,3,2,3,4,3,4,5

Cheers for any help :-)


September 8th, 2015 2:23am

This works for me:

$lines = gc input.txt

foreach ($line in $lines) {
  $tokens = $line -split '[ ,]'
  $n = 0
  foreach ($token in $tokens) {
    if ($n -ne 0) {
      if ( ( '' + ((get-variable -name $tokens[0] -ea 0).value) -eq '') ) {
        $d = ''
       }
      else {
        $d = ','
       }
      set-variable -name $tokens[0] -value (
         '' + ((get-variable -name $tokens[0] -ea 0).value) + $d + $token )
     } 
    $n++
   }
 }

$o = @()

foreach ($line in $lines) {
  $tokens = $line -split '[ ,]'
  $o += $tokens[0] + ' ' + (get-variable -name $tokens[0] -ea 0).value
 }

$o | Sort-Object | Select-Object -Unique

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 9:11pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics