比较行和打印相同的值相同的行 [英] compare rows and print the same values for the same rows
问题描述
我有这样一个输入:
A 118791136点¯x
一118791136点¯x
一118791136点¯x
一118791136点¯x
乙118791136点¯x
乙23456433点¯x
乙23456433点¯x
乙23456433点¯x
。
。
。
我想将其转换为输出,如:
A 118791136 XXXXX
乙23456433 XXX
。
。
。
我知道一点点的Perl,但我不知道如何行比较行
非常感谢你提前
如果我们有
A 118791136点¯x
一23456433点¯x
乙34222223点¯x
乙56444233点¯x
怎么可能有这样的输出:
A 118791136,23456433 X,X
乙34222223,56444233 X,X
...
...
这件使得它:
$ AWK'{a [$ 1,$ 2] = A [$ 1,$ 2] $ 3} END {为(我的){打印I,A [I]}}'文件
B118791136点¯x
A118791136 XXXX
B23456433 XXX
只是存储在一个数组的结果,其第一和第二场为指标。最后,它打印的结果。
结果给出 B23456433
而不是 B 23456433
,试图分裂它... sed的
使得它:
$ AWK'{a [$ 1,$ 2] = A [$ 1,$ 2] $ 3} END {为(我的){打印I,A [I]}}'文件| SED的/ \\([A-Z] \\)/ \\ 1 /'
乙118791136点¯x
一118791136 XXXX
乙23456433 XXX
根据sudo_O's评论
$ AWK'{a [$ 1,$ 2] = A [$ 1,$ 2] $ 3} END {为(我的){拆分(I,B,SUBSEP);印片B [1],B [2],A []}}'文件
乙118791136点¯x
一118791136 XXXX
乙23456433 XXX
更新基于新的注释
@fedorqui是有可能增加一个计数器,以某种方式计数的数目
一列中相同的行?
块引用>$ AWK'{a [$ 1,$ 2] = A [$ 1,$ 2] $ 3} END {为(我的){拆分(I,B,SUBSEP);印片B [1],B [2],A [I]中,长度(一个由[i])}}'文件
乙118791136×1
一118791136 XXXX 4
乙23456433 XXX 3I have an input like this:
A 118791136 X A 118791136 x A 118791136 X A 118791136 X B 118791136 x B 23456433 X B 23456433 X B 23456433 x . . .
I would like to convert it to an output like:
A 118791136 XxXXx B 23456433 XXx . . .
I know a little bit perl, but I do not know how to compare row by row
Thank you very much in advance
AND if we have
A 118791136 X A 23456433 x B 34222223 X B 56444233 X
How it is possible to have an output like this:
A 118791136,23456433 X,x B 34222223,56444233 X,X ... ...
解决方案This piece makes it:
$ awk '{a[$1,$2]=a[$1,$2]$3} END{for (i in a) {print i, a[i]}}' file B118791136 x A118791136 XxXX B23456433 XXx
Just stores the result in an array, having 1st and 2nd fields as indexes. At the end, it prints the result.
The result gives
B23456433
instead ofB 23456433
, trying to split it...sed
makes it:$ awk '{a[$1,$2]=a[$1,$2]$3} END{for (i in a) {print i, a[i]}}' file | sed 's/\([A-Z]\)/\1 /' B 118791136 x A 118791136 XxXX B 23456433 XXx
Update based on sudo_O's comment
$ awk '{a[$1,$2]=a[$1,$2]$3} END{for (i in a) {split(i,b,SUBSEP); print b[1], b[2], a[i]}}' file B 118791136 x A 118791136 XxXX B 23456433 XXx
Update based on new comment
@fedorqui is it possible to add a counter, somehow count the number of the same rows in a column?
$ awk '{a[$1,$2]=a[$1,$2]$3} END{for (i in a) {split(i,b,SUBSEP); print b[1], b[2], a[i], length(a[i])}}' file B 118791136 x 1 A 118791136 XxXX 4 B 23456433 XXx 3
这篇关于比较行和打印相同的值相同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!