awk-仅对非空字段执行列操作

awk-仅对非空字段执行列操作,awk,Awk,我想对csv文件进行一些更改。该文件如下所示: csv文件: 04.08.1994 22:47;3.3;29;;61.8;1;4.0;1.4;433; 04.08.1994 23:11;;27;93.0;60.6;2;2.0;6.5;133; 04.08.1994 23:48;3.1;;18.4;93.1;1;1.0;6.8;; 期望输出: 04.08.1994 22:47;33;29;12345;61.8;20;4.0;1.4;433; 04.08.1994 23:11;12345;27;9

我想对csv文件进行一些更改。该文件如下所示:

csv文件:

04.08.1994 22:47;3.3;29;;61.8;1;4.0;1.4;433;
04.08.1994 23:11;;27;93.0;60.6;2;2.0;6.5;133;
04.08.1994 23:48;3.1;;18.4;93.1;1;1.0;6.8;;
期望输出:

04.08.1994 22:47;33;29;12345;61.8;20;4.0;1.4;433;
04.08.1994 23:11;12345;27;930;60.6;40;2.0;6.5;133;
04.08.1994 23:48;31;12345;184;93.1;20;1.0;6.8;12345;
应发生以下情况:

  • 将空字段替换为特殊值,例如“12345”

  • 仅对非空字段执行某些列的算术运算(乘以10或20)

  • 我的解决方案:

    cat file | awk 'BEGIN {FS=OFS=";"} { for(i=1; i<=NF; i++) if($i ~ /^ *$/) $i = 12345 }; \
    { $2!=12345 && ($2=$2*10); $4!=12345 && ($4=$4*10); $6!=12345 && ($6=$6*20); print}'
    

    cat文件| awk'BEGIN{FS=OFS=“;”}{for(i=1;i请尝试以下内容(用GNU
    awk测试并编写)

    另一个awk:

    $ awk '
    BEGIN {
        FS=OFS=";"
        some[2]=some[4]=10        # some columns 
        some[6]=20                # defined
    }
    {
        for(i=1;i<NF;i++)
            if($i=="")            # Replace empty fields 
                $i="12345"        # with a special value, say "12345".
            else if(i in some)    # on non-empty fields
                $i*=some[i]       # Do arithmetic operations of some columns
    }1' file
    
    请将示例输入(无描述、无图像、无链接)和该示例输入的所需输出添加到您的问题中(无评论)。
    04.08.1994 22:47;33;29;12345;61.8;20;4.0;1.4;433;
    04.08.1994 23:11;12345;27;930;60.6;40;2.0;6.5;133;
    04.08.1994 23:48;31;12345;184;93.1;20;1.0;6.8;12345;
    
    $ awk '
    BEGIN {
        FS=OFS=";"
        some[2]=some[4]=10        # some columns 
        some[6]=20                # defined
    }
    {
        for(i=1;i<NF;i++)
            if($i=="")            # Replace empty fields 
                $i="12345"        # with a special value, say "12345".
            else if(i in some)    # on non-empty fields
                $i*=some[i]       # Do arithmetic operations of some columns
    }1' file
    
    04.08.1994 22:47;33;29;12345;61.8;20;4.0;1.4;433;
    04.08.1994 23:11;12345;27;930;60.6;40;2.0;6.5;133;
    04.08.1994 23:48;31;12345;184;93.1;20;1.0;6.8;12345;