使用 awk 合并行

使用 awk 合并行

我有一个输入文件,其中包含以下模式的大量数据。部分数据如下所示:

Data1 
C
In;
CP
In;
D
In;
Q
Out;
Data2 
CP
In;
D
In;
Q
Out;
Data3 
CP
In;
CPN
In;
D
In;
QN
Out;

我希望我的输出为

Data1(C,CP,D,Q)
In C;
In CP;
In D;
Out Q;
Data2 (CP,D,Q)
In CP;
In D;
Out Q;
Data3 (CP,CPN,D,QN)
In CP;
In CPN;
In D;
Out QN;

我怎样才能做到这一点。

答案1

$ cat tst.awk
BEGIN { FS="[[:space:];]+" }
{ rec[++nf] = $1 }
$1 == "Out" {
    printf "%s(", rec[1]
    for (i=2; i<=nf; i+=2) {
        printf "%s%s", (i>2 ? "," : ""), rec[i]
    }
    print ")"

    for (i=2; i<=nf; i+=2) {
        print rec[i+1], rec[i] ";"
    }

    delete rec
    nf = 0
}

$ awk -f tst.awk file
Data1(C,CP,D,Q)
In C;
In CP;
In D;
Out Q;
Data2(CP,D,Q)
In CP;
In D;
Out Q;
Data3(CP,CPN,D,QN)
In CP;
In CPN;
In D;
Out QN;

答案2

尝试这个...

awk 'BEGIN {RS="\n\n";} {for(i=1;i<=NF;i++) if ($i ~ "Data") print $i; else if ($i ~ "In;") print "In " $(i-1)";" ; else if ($i ~ "Out") print "Out " $(i-1)";"}'  a.txt

答案3

GNU sed使用扩展正则表达式模式调用-E,我们可以打印端口及其类型,如图所示。欲了解更多信息。您可以查找联机帮助页 (man sed) 或 Gnu sed 手册 (info sed)

$ sed -Ee '
  /\n/{
    /\n.*\n/{P;D;}
    p;$d;g
  }
  :loop
    $break;N
  /;(\n[^;]+){2}$/!bloop
  :reak
  h;s/;[^;]+$/;/
  s/\n[^\n;]+;//g
  s/\n/(/;y/\n/,/;s/$/)/p
  g;s/.*;\n//
  x;s/;[^;]+$/;/
  s/\n([^\n]+)(\n[^\n;]+)/\2 \1/g
  D
' file

结果:

Data1 (C,CP,D,Q)
In C;
In CP;
In D;
Out Q;
Data2 (CP,D,Q)
In CP;
In D;
Out Q;
Data3 (CP,CPN,D,QN)
In CP;
In CPN;
In D;
Out QN;

答案4

gawk '
    BEGIN { FS = "\n"; RS = ";\n" }
    function output() {
        print data ")"
        for (i=1; i<=c; i++) print inout[i]
        delete inout
        c=0
    }
    NF == 3 {
        if (NR > 1) output()
        data = $1 "(" $2
        inout[++c] = $3 " " $2 ";"
    }
    NF == 2 {
        data = data "," $1
        inout[++c] = $2 " " $1 ";"
    }
    END {output()}
' file
Data1 (C,CP,D,Q)
In C;
In CP;
In D;
Out Q;
Data2 (CP,D,Q)
In CP;
In D;
Out Q;
Data3 (CP,CPN,D,QN)
In CP;
In CPN;
In D;
Out QN;

相关内容