AWK合并线累加器不工作

时间:2018-05-30 00:10:28

标签: awk

下面是分为2行的单个记录,在第3个字段后有一个嵌入的换行符(空白行是嵌入的换行符)

peter,9,ghi

mno

算法是,如果记录中的字段少于4个,则继续合并后续行,直到有4个字段,然后输出记录。

我有awk代码,据说这样做。 2例。

案例1

> If the number of fields in the current line plus the accumulated
> number of fields = 4 then     if there were no previous fields 
>        print current line      else 
>       print previously accumulated line plus current line

CASE 2 将当前行追加到累计的前一行

BEGIN {
  FS=","
  flds=4
  prevF=0
}
flds == NF + prevF {
                    print (prevF==0) ?  $0 :  prevLine $0
                    prevF=0
                    prevLine=""
                    next
                   }
{prevLine = (prevF==0) ? $0 FS : prevLine $0
 prevF = prevF + NF}

足够简单的算法。当我针对数据片段运行时,我得到了

,mnohi,9,ghi

而不是第二行加到第一行的末尾。

我有兴趣理解为什么代码在awk解决方案中的行为。

0 个答案:

没有答案