简体   繁体   English

在bash中转置多列

[英]transposing multiple columns in bash

demilited file like this 像这样的半成品文件

Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764

and I wish to convert them into 我希望将它们转换为

Input1 file1:45764, file1:878755, file1: 899787 
Input2 file1:45676, file1:769678, file1: 6454764 

Any guess? 有猜到吗? Thanks in advance 提前致谢

awk '{b[$1]=b[$1] $2$3"  "}END{for (i in b) print i,b[i]}' inputFile

will produce output as 将产生输出为

Input1 file1:45764  file1:878755  file1:899787  
Input2 file1:45676  file1:769678  file1:6454764  

what it does? 它能做什么?

{b[$1]=b[$1] $2$3" "} creates an array b appends the second and third column(since there was some spaces between file and value in your example). {b[$1]=b[$1] $2$3" "}创建一个数组b追加第二和第三列(因为示例中filevalue之间存在一些空格)。 $2$3 into the array. $2$3放入阵列。 The array is an associative array indexed by the Inputx where x is 1,2... 该数组是一个由Inputx索引的关联数组,其中x1,2...

that is 那是

b['Input1'] = 'file1:45764  file1:878755  file1:899787'

END block is excecuted at end of input file, input , END块在输入文件input末尾执行,

for (i in b) print i,b[i]} prints the content of b array for (i in b) print i,b[i]}打印b数组的内容

And if you want those commas in the output, try 如果要在输出中使用逗号,请尝试

awk '{if(b[$1])b[$1] = b[$1]", "; b[$1] = b[$1] $2 $3}; END{for(i in b)print i, b[i]}'

or the slightly terser 或稍微更简短

awk '{b[$1]=b[$1](b[$1]?", ":"")$2$3}END{for(i in b)print i,b[i]}'

output 产量

Input1 file1:45764, file1:878755, file1:899787
Input2 file1:45676, file1:769678, file1:6454764

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM