比较连续行中不在同一列中的两个字段 [英] compare two fields not in the same column in consecutive lines

查看:81
本文介绍了比较连续行中不在同一列中的两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了论坛,但没有找到问题的答案. 我有一个看起来像这样的文件:

I searched the forum but did not found the answer to my question. I have a file that look like this :

chr left_pos    right_pos
1   2596    3084
1   3084    5869
1   18500   21000
3   21000   21536
3   22872   23179

我已经在第1列然后第2列对文件进行了排序: 如果第一列中的字段相同,我想在下一行中将字段3中的数字与字段2中的数字进行比较.如果是,则在行尾添加等于的标签.如果不等于,则不等于.

I already sorted ou the file on column one then column 2: I would like to compare the number in field 3 to the number in field 2 in the following line if the field in column one are identical. If yes add the label equal at the end of the line. If not add not equal.

输出应如下所示

chr left_pos    right_pos   
1   2596    3084    not_equal
1   3084    5869    equal
1   18500   21000   not_equal
3   21000   21536   not_equal
3   22872   23179   not_equal

感谢您的帮助

推荐答案

这应该对您有用:

awk 'NR==1 { print; next } { print $0, ($1 == a && $2 == b) ? "equal" : "not_equal"; a = $1; b = $3 }' file | column -t

结果:

chr  left_pos  right_pos
1    2596      3084       not_equal
1    3084      5869       equal
1    18500     21000      not_equal
3    21000     21536      not_equal
3    22872     23179      not_equal

这篇关于比较连续行中不在同一列中的两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆