使用awk计算字段中的多个记录

时间:2017-04-20 18:47:43

标签: awk count

我有一个像这样的大文件

C1,C2,C3
C1,C2
C5
C3,C5

我期待一个这样的输出

C1,C2,C3   3
C1,C2      2
C5         1
C3,C5      2

我想用shell做这个。你能帮帮我们吗? 三江源

2 个答案:

答案 0 :(得分:2)

这样的东西
class SCConstraintLayout extends android.support.constraint.ConstraintLayout {
    public SCConstraintLayout(Context context) {
        super(context);
    }

    public SCConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SCConstraintLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int chosenDimension = 0;
        int mode = 0;

        if (ScMain.isLandscapeOriented) {
            chosenDimension = MeasureSpec.getSize(heightMeasureSpec);
            mode = MeasureSpec.getMode(heightMeasureSpec);
        } else {
            chosenDimension =  MeasureSpec.getSize(widthMeasureSpec);
            mode = MeasureSpec.getMode(widthMeasureSpec);
        }

        int width = MeasureSpec.makeMeasureSpec(chosenDimension, mode);
        int height = MeasureSpec.makeMeasureSpec(chosenDimension, mode);

        //        setMeasuredDimension(width, height);
        super.onMeasure(width, height);
    }
}

应该给出

awk 'BEGIN{FS=","}{printf "%-20s\t%d\n",$0,NF;}' file

注意您需要根据行的最大长度逻辑调整宽度

答案 1 :(得分:1)

awk中的另一个人:

$ awk '{
    m=(m<(n=length($0))?n:m)                  # get the max record length
    a[NR]=$0 }                                # hash to a
END {
    for(i=1;i<=NR;i++)                        # iterate and (below) output nicely
        printf "%s%"(m-length(a[i])+4)"s\n",a[i],gsub(/,/,"&",a[i])+1 }
' file
C1,C2,C3   3
C1,C2      2
C5         1
C3,C5      2

如果您想要更改字段与长度之间的距离,请使用+4中的printf玩具。

相关问题