在AWK中删除数组的特定元素

时间:2015-11-12 12:48:07

标签: arrays awk

我有一个关联数组,我需要从中删除特定元素。 该数组是从包含500行的制表符分隔文件的[11到16]列创建的。

要删除的元素是" coche"

Array content:"coche"   ""  ""  ""  ""  ""
              "puerta"  "coche" ""  ""  ""  ""
              "cabra"   "puerta"    "coche" ""  ""  ""
              "coche"   ""  ""  ""  ""  ""
              "limpia"  "coche" ""  ""  ""  ""
              "cabra"   "puerta"    "cajon" "coche" ""  ""
              "cazo"    "coche" ""  ""  ""  ""
              "puerta"  "coche" ""  ""  ""  ""
              "coche"   ""  ""  ""  ""  ""

除非该行仅包含" coche"。

Example: "coche"    ""  ""  ""  ""  ""

预期结果

New array contents:
"coche" ""  ""  ""  ""  ""
"puerta"    ""  ""  ""  ""  ""
"cabra" "puerta"    ""  ""  ""  ""
"coche" ""  ""  ""  ""  ""
"limpia"    ""  ""  ""  ""  ""
"cabra" "puerta"    "cajon" ""  ""  ""
"cazo"  ""  ""  ""  ""  ""
"puerta"    ""  ""  ""  ""  ""
"coche" ""  ""  ""  ""  ""

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

t=split(a[g], A, "\t");
for (ix=2; ix<=t; ix++)
{
    if (match(A[ix],"\"coke\"")>0)
    {
       if (t>2)
       {
          for (jx=ix+1; jx <=t; jx++) 
          {
                A[jx-1]=A[jx]; # This is the formula that I was looking\
                                       to remove the match element
          }
          t--;
       }
}

    }

        a[g]="";
        for (ix=2; ix<=t; ix++)
        {
            a[g]=a[g]"\t"A[ix];
        }


        for (y=t; y<7; y++)
        {
            a[g]=a[g]"\t"x;
        }
相关问题