如果两列中的值相同,请选择当前行和上一行

时间:2019-02-17 18:20:16

标签: awk

检查第2列和第3列中的值,如果上一行和当前行中的值相同(例如第2-3和6-7行),则打印以分隔的行,

输入文件

1   1   2 35  1
2   3   4 50  1
2   3   4 75  1
4   7   7 85  1
5   8   6 100 1
8   6   9 125 1
4   6   9 200 1
5   3   2 156 2

所需的输出

2,3,4,50,1,2,3,4,75,1
8,6,9,125,1,4,6,9,200,1

我尝试修改此代码,但未修改结果

awk '{$6=$2 $3 - $p2 $p3} $6==0{print p0; print} {p0=$0;p2=p2;p3=$3}'

谢谢。

3 个答案:

答案 0 :(得分:2)

$ awk -v OFS=',' '{$1=$1; cK=$2 FS $3} pK==cK{print p0, $0} {pK=cK; p0=$0}' file
2,3,4,50,1,2,3,4,75,1
8,6,9,125,1,4,6,9,200,1

答案 1 :(得分:1)

您自己的代码及其机制已更新:

awk '(($2=$2) $3) - (p2 p3)==0{printf "%s", p0; print} {p0=$0;p2=$2;p3=$3}' OFS="," file
2,3,4,50,12,3,4,75,1
8,6,9,125,14,6,9,200,1

但是它有潜在的问题,所以最好使用这种简化/改进的方式:

awk '($2=$2) FS $3==cp{print p0,$0} {p0=$0; cp=$2 FS $3}' OFS=, file

需要FS,请检查Mr. Morton's answer下的注释。

代码为何失败:

  • 串联(做什么)的优先级高于负-
  • 您使用$6保存了要比较的值,然后它成为该行$0的一部分。(最后一列)。 -您可以将其更改为临时变量名称。
  • 您有一个错字(p2=p2),并且使用了$p2$p3,这意味着获取p2的值并找到对应的列。因此,如果p2==3等于$p2
  • 您未设置$3,因此即使您的代码有效,输出也会被OFS空格分隔。
  • 将添加尾随换行符print,因此,即使上述问题不存在,您也将得到4行而不是所需的2行输出。

答案 2 :(得分:1)

Could you please try following too.

awk 'prev_2nd==$2 && prev_3rd==$3{$1=$1;print prev_line,$0} {prev_2nd=$2;prev_3rd=$3;$1=$1;prev_line=$0}' OFS=,  Input_file

Explanation: Adding explanation for above code now.

awk '
prev_2nd==$2 && prev_3rd==$3{     ##Checking if previous lines variable prev_2nd and prev_3rd are having same value as current line 2nd and 3rd field or not, if yes then do following.
  $1=$1                           ##Resetting $1 value of current line to $1 only why because OP needs output field separator as comma and to apply this we need to reset it to its own value.
  print prev_line,$0              ##Printing value of previous line and current line here.
}                                 ##Closing this condition block here.
{
  prev_2nd=$2                     ##Setting current line $2 to prev_2nd variable here.
  prev_3rd=$3                     ##Setting current line $3 to prev_3rd variable here.
  $1=$1                           ##Resetting value of $1 to $1 to make comma in its values applied.
  prev_line=$0                    ##Now setting pre_line value to current line edited one with comma as separator.
}
' OFS=,  Input_file               ##Setting OFS(output field separator) value as comma here and mentioning Input_file name here.