Skip to main content
Post Made Community Wiki by don_crissti
duh...
Source Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

With gnu datamash and column from util-linux:

tr : \\t <infile | datamash -t $'\t': transpose <infile | column -t -s $'\t':

This works with more than two columns but assumes there are no empty lines in your input file; with empty lines in between (like in your initial input sample) you would get an error like:

datamash: transpose input error: line 2 has 0 fields (previous lines had 2);

so to avoid that you'll have to squeeze them before processing with datamash (this time using a literal TAB between single quotes):

tr : \\t <infile | tr -s \\n <infile | datamash -t'      't: transpose | column -t -s'      's:

Otherwise, in this particular case (only two columns), with zsh and the same column:

list=(${(f)"$(<infile)"})
printf %s\\n ${(j;:;)list[@]%:*} ${(j;:;)list[@]#*:} | column -t -s:

(${(f)"$(<infile)"}) reads the lines in an array; ${(j;:;)list[@]%:*} joins (with :) the first field of each element and ${(j;:;)list[@]#*:} joins (again with :) the second field of each element; these are both printed, e.g. the output is

Virtual_Machine:ID:Status:Memory:Uptime:Server:Pool:HA:VCPU:Type:OS
OL6U7:0004fb00000600003da8ce6948c441bd:Running:65536:17103:MyOVS1.vmworld.com:HA-POOL:false:16:Xen PVM:Oracle Linux 6

which is then piped to column -t -s:

With gnu datamash and column from util-linux:

tr : \\t <infile | datamash -t $'\t' transpose | column -t -s $'\t'

This works with more than two columns but assumes there are no empty lines in your input file; with empty lines in between (like in your initial input sample) you would get an error like:

datamash: transpose input error: line 2 has 0 fields (previous lines had 2);

so to avoid that you'll have to squeeze them before processing with datamash (this time using a literal TAB between single quotes):

tr : \\t <infile | tr -s \\n | datamash -t'      ' transpose | column -t -s'      '

Otherwise, in this particular case (only two columns), with zsh and the same column:

list=(${(f)"$(<infile)"})
printf %s\\n ${(j;:;)list[@]%:*} ${(j;:;)list[@]#*:} | column -t -s:

(${(f)"$(<infile)"}) reads the lines in an array; ${(j;:;)list[@]%:*} joins (with :) the first field of each element and ${(j;:;)list[@]#*:} joins (again with :) the second field of each element; these are both printed, e.g. the output is

Virtual_Machine:ID:Status:Memory:Uptime:Server:Pool:HA:VCPU:Type:OS
OL6U7:0004fb00000600003da8ce6948c441bd:Running:65536:17103:MyOVS1.vmworld.com:HA-POOL:false:16:Xen PVM:Oracle Linux 6

which is then piped to column -t -s:

With gnu datamash and column from util-linux:

datamash -t: transpose <infile | column -t -s:

This works with more than two columns but assumes there are no empty lines in your input file; with empty lines in between (like in your initial input sample) you would get an error like:

datamash: transpose input error: line 2 has 0 fields (previous lines had 2);

so to avoid that you'll have to squeeze them before processing with datamash:

tr -s \\n <infile | datamash -t: transpose | column -t -s:

Otherwise, in this particular case (only two columns), with zsh and the same column:

list=(${(f)"$(<infile)"})
printf %s\\n ${(j;:;)list[@]%:*} ${(j;:;)list[@]#*:} | column -t -s:

(${(f)"$(<infile)"}) reads the lines in an array; ${(j;:;)list[@]%:*} joins (with :) the first field of each element and ${(j;:;)list[@]#*:} joins (again with :) the second field of each element; these are both printed, e.g. the output is

Virtual_Machine:ID:Status:Memory:Uptime:Server:Pool:HA:VCPU:Type:OS
OL6U7:0004fb00000600003da8ce6948c441bd:Running:65536:17103:MyOVS1.vmworld.com:HA-POOL:false:16:Xen PVM:Oracle Linux 6

which is then piped to column -t -s:

added 806 characters in body
Source Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

With gnu datamashgnu datamash and column from util-linux:

tr : \\t <infile | datamash -t $'\t' transpose | column -t -s $'\t'

thisThis works with more than two columns but assumes there are no empty lines in your input file; with empty lines in between (like in your initial input sample) you would get an error like:

datamash: transpose input error: line 2 has 0 fields (previous lines had 2);

so to avoid that you'll have to squeeze them before processing with datamash (this time using a literal TAB between single quotes):

tr : \\t <infile | tr -s \\n | datamash -t'      ' transpose | column -t -s'      '

Otherwise, in this particular case (only two columns), with zsh and the same column:

list=(${(f)"$(<infile)"})
printf %s\\n ${(j;:;)list[@]%:*} ${(j;:;)list[@]#*:} | column -t -s:

(${(f)"$(<infile)"}) reads the lines in an array; ${(j;:;)list[@]%:*} joins (with :) the first field of each element and ${(j;:;)list[@]#*:} joins (again with :) the second field of each element; these are both printed, e.g. the output is

Virtual_Machine:ID:Status:Memory:Uptime:Server:Pool:HA:VCPU:Type:OS
OL6U7:0004fb00000600003da8ce6948c441bd:Running:65536:17103:MyOVS1.vmworld.com:HA-POOL:false:16:Xen PVM:Oracle Linux 6

which is then piped to column -t -s:

With gnu datamash and column from util-linux:

tr : \\t <infile | datamash -t $'\t' transpose | column -t -s $'\t'

this assumes there are no empty lines in your input file; with empty lines in between (like in your input sample) you would get an error like:

datamash: transpose input error: line 2 has 0 fields (previous lines had 2);

so to avoid that you'll have to squeeze them before processing with datamash (this time using a literal TAB between single quotes):

tr : \\t <infile | tr -s \\n | datamash -t'      ' transpose | column -t -s'      '

With gnu datamash and column from util-linux:

tr : \\t <infile | datamash -t $'\t' transpose | column -t -s $'\t'

This works with more than two columns but assumes there are no empty lines in your input file; with empty lines in between (like in your initial input sample) you would get an error like:

datamash: transpose input error: line 2 has 0 fields (previous lines had 2);

so to avoid that you'll have to squeeze them before processing with datamash (this time using a literal TAB between single quotes):

tr : \\t <infile | tr -s \\n | datamash -t'      ' transpose | column -t -s'      '

Otherwise, in this particular case (only two columns), with zsh and the same column:

list=(${(f)"$(<infile)"})
printf %s\\n ${(j;:;)list[@]%:*} ${(j;:;)list[@]#*:} | column -t -s:

(${(f)"$(<infile)"}) reads the lines in an array; ${(j;:;)list[@]%:*} joins (with :) the first field of each element and ${(j;:;)list[@]#*:} joins (again with :) the second field of each element; these are both printed, e.g. the output is

Virtual_Machine:ID:Status:Memory:Uptime:Server:Pool:HA:VCPU:Type:OS
OL6U7:0004fb00000600003da8ce6948c441bd:Running:65536:17103:MyOVS1.vmworld.com:HA-POOL:false:16:Xen PVM:Oracle Linux 6

which is then piped to column -t -s:

Source Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

With gnu datamash and column from util-linux:

tr : \\t <infile | datamash -t $'\t' transpose | column -t -s $'\t'

this assumes there are no empty lines in your input file; with empty lines in between (like in your input sample) you would get an error like:

datamash: transpose input error: line 2 has 0 fields (previous lines had 2);

so to avoid that you'll have to squeeze them before processing with datamash (this time using a literal TAB between single quotes):

tr : \\t <infile | tr -s \\n | datamash -t'      ' transpose | column -t -s'      '