比较连续行中不在同一列中的两个字段

编程入门 行业动态 更新时间:2024-10-27 01:26:41
本文介绍了比较连续行中不在同一列中的两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

比较连续行中不在同一列中的两个字段

本文发布于:2023-10-31 07:58:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   中不   两个   在同一

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!