根据column1连接多行


8

我有一个如下文件。

abc, 12345
def, text and nos    
ghi, something else   
jkl, words and numbers

abc, 56345   
def, text and nos   
ghi, something else 
jkl, words and numbers

abc, 15475  
def, text and nos 
ghi, something else
jkl, words and numbers

abc, 123345
def, text and nos
ghi, something else  
jkl, words and numbers

我想将其转换(加入)为:

abc, 12345, 56345, 15475, 123345
def, text and nos, text and nos,text and nos,text and nos
ghi, something else, something else, something else, something else   
jkl, words and numbers, words and numbers, words and numbers, words and numbers

2
您的输入文件中是否确实有多余的空行?如果不是,请编辑并删除它们,你应该表现出的文件完全相同,因为它是。
terdon

Answers:


10

如果您不介意输出顺序:

$ awk -F',' 'NF>1{a[$1] = a[$1]","$2};END{for(i in a)print i""a[i]}' file 
jkl, words and numbers, words and numbers, words and numbers, words and numbers
abc, 12345, 56345, 15475, 123345
ghi, something else, something else, something else, something else
def, text and nos, text and nos, text and nos, text and nos

说明

  • NF>1 这意味着我们只需要处理非空白的行。
  • 我们将所有第一个字段保存在关联数组中a,键是第一个字段,值是第二个字段(或行的其余部分)。如果键已经具有值,则我们合并两个值。
  • END块中,我们遍历关联数组a,并用相应的值打印其所有键。

或使用perl将保持顺序:

$perl -F',' -anle 'next if /^$/;$h{$F[0]} = $h{$F[0]}.", ".$F[1];
    END{print $_,$h{$_},"\n" for sort keys %h}' file
abc, 12345, 56345, 15475, 123345

def, text and nos, text and nos, text and nos, text and nos

ghi, something else, something else, something else, something else

jkl, words and numbers, words and numbers, words and numbers, words and numbers

您从我的问题unix.stackexchange.com/questions/124181/中获得的 Perl解决方案也应该正常工作吗?
Ramesh 2014年

否。OP希望根据第1列连接字符串,无论是否重复。您的问题不想重复。
cuonglm

哦好的。乍一看,这几乎与我的问题相似。:)
Ramesh 2014年

1
整洁,+ 1!但这并不能保持顺序,它只是在此特定示例(字段按字母顺序排列)中重新创建。
terdon

只是为了笑,我写几乎完全同样的方法读你的答案之前:perl -F, -lane 'next unless /./;push @{$k{$F[0]}}, ",@F[1..$#F]"; END{print "$_@{$k{$_}}" foreach keys(%k)}' file:)英雄所见略同!
terdon

1

哦,这很简单。这是一个简单的版本,可以保持键在文件中的显示顺序:

$ awk -F, '
    /.+/{
        if (!($1 in Val)) { Key[++i] = $1; }
        Val[$1] = Val[$1] "," $2; 
    }
    END{
        for (j = 1; j <= i; j++) {
            printf("%s %s\n%s", Key[j], Val[Key[j]], (j == i) ? "" : "\n");       
        }                                    
    }' file.txt

输出应如下所示:

abc, 12345, 56345, 15475, 123345

def, text and nos, text and nos, text and nos, text and nos

ghi, something else, something else, something else, something else

jkl, words and numbers, words and numbers, words and numbers, words and numbers

如果你不介意在最后一个额外的空白行,只需更换printf与线printf("%s %s\n\n", Key[j], Val[Key[j]]);

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.