Awk 如果两列中的值相同,请选择“当前行”和“上一行”
检查第2列和第3列中的值,如果前一行和当前行(例如第2-3行和第6-7行)中的值相同,则将分隔的行打印为 输入文件Awk 如果两列中的值相同,请选择“当前行”和“上一行”,awk,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 我试图修改此代码,但没
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}'
提前感谢。更新了您自己的代码及其机制:
$ 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
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
,请检查下面的注释
代码失败的原因:
- 连接(空间的作用)的优先级高于负
李>-
- 您使用
保存要比较的值,然后它成为$6
行(最后一列)的一部分您可以将其更改为临时变量名$0
- 您有一个输入错误(
),您使用了p2=p2
和$p2
,这意味着获取$p3
的值并找到相应的列。因此,如果p2
,那么p2==3
等于$p2
$3
- 您没有设置OFS,因此即使您的代码正常工作,输出也将被
空格分隔
将添加一个尾随换行符print
,因此即使不存在上述问题,您也将得到4行而不是想要的2行输出\n
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
,请检查下面的注释
代码失败的原因:
- 连接(空间的作用)的优先级高于负
李>-
- 您使用
保存要比较的值,然后它成为$6
行(最后一列)的一部分您可以将其更改为临时变量名$0
- 您有一个输入错误(
),您使用了p2=p2
和$p2
,这意味着获取$p3
的值并找到相应的列。因此,如果p2
,那么p2==3
等于$p2
$3
- 您没有设置OFS,因此即使您的代码正常工作,输出也将被
空格分隔
将添加一个尾随换行符print
,因此即使不存在上述问题,您也将得到4行而不是想要的2行输出\n
- 请您也尝试一下以下内容
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
解释:现在为上述代码添加解释
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.
请你也试试下面的
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
解释:现在为上述代码添加解释
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.
为什么在
$2fs$3
中使用FS
?既然它只是用于比较,FS
不是多余的吗?@Tiw一个分隔符是必要的,因为a bc
和ab c
都连接到abc
,如果你不包括一个。为什么FS
在$2 FS$3
中?既然它只是用于比较,FS
不是多余的吗?@Tiw一个分隔符是必要的,因为a bc
和ab c
都连接到abc
,如果你没有包含一个。@OXXO,如果这对你也有帮助,你能检查一下吗?@OXXO,如果这对你也有帮助,你能检查一下吗?