差分连续线路unix



我有一个扁平行中的树,如:

a<1 and b<1 and c<1 then result=1
a<1 and b>1 and d<1 then result=2
a<1 and b>1 and d>1 then result=3

我想打印删除与前一行匹配的每一行的子字符串例如,结果将是:

a<1 and b<1 and c<1 then result=1
        b>1 and d<1 then result=2
                d>1 then result=3

本质上,上一行和当前行之间的公共元素不会再次打印->只打印两行之间的差异。

有人能帮忙吗?

可选,使用字段作为匹配单位,最终输出格式化

awk 'NR==1{w=length($0)} 
     {sep=line=""; 
      for(i=1;i<=NF;i++) 
        if(p[i]!=$i) 
          for(j=i;j<=NF;j++) {
             p[j]=$j; 
             line=line sep $j;
             sep=OFS
          } 
         printf "%"w"sn", line
      }' diffs
a<1 and b<1 and c<1 then result=1
        b>1 and d<1 then result=2
                d>1 then result=3
awk '{for (i=1;i<=length($0); i++) 
    if (substr($0,i,1)!=substr(a,i,1)) {printf "%s",substr($0,i,1);a=""}
      else printf " ";
    printf "n"
    a=$0}'

产生

a<1 and b<1 and c<1 then result=1
         >1 and d<1 then result=2
                 >1 then result=3

即前一行和当前行之间的公共字符不再打印

如果你需要你发布的结果,你可以分割你的行以形成标记,并将这些标记与前一行的标记进行比较。您可以打印令牌或必要的空格来获得标识。

最新更新