移调在bash多列 [英] transposing multiple columns in bash

查看:174
本文介绍了移调在bash多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

demilited文件中像这样

 输入1文件1:45764
输入1文件1:878755
输入1文件1:899787
INPUT2文件1:45676
INPUT2文件1:769678
INPUT2文件1:6454764

和我想它们转换成

 输入1文件1:45764,文件1:878755,文件1:899787
INPUT2文件1:45676,文件1:769678,文件1:6454764

任何的猜测?在此先感谢


解决方案

 的awk'{B [$ 1] = B [$ 1] $ 2 $ 3} END {了(我在B )打印I,b [I]}'INPUTFILE

将产生输出

 输入1文件1:45764文件1:878755文件1:899787
INPUT2文件1:45676文件1:769678文件1:6454764

它做什么?

{B [$ 1] = B [$ 1] $ 2 $ 3} 创建一个数组 B 追加第二和第三列(因为当时文件在你的榜样之间的一些空格)。 $ 2 $ 3 入阵。该阵列是由 Inputx索引的关联数组,其中 X 1,2 ..

这是

  B ['输入1'] ='文件1:45764文件1:878755文件1:899787

END 块在输入文件结束excecuted,输入

的(我在二)印刷,B [I]} 打印数组内容

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 betweenfile and value in your example).$2$3 into the array. The array is an associative array indexed by the Inputx where x is 1,2...

that is

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

END block is excecuted at end of input file, input,

for (i in b) print i,b[i]} prints the content of b array

这篇关于移调在bash多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆