compare two fields not in the same column in consecutive lines

好久不见. 提交于 2019-12-11 02:04:37

问题


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

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.

The output should look like this

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

Thanks for your help


回答1:


This should work for you:

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

Results:

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


来源:https://stackoverflow.com/questions/13851614/compare-two-fields-not-in-the-same-column-in-consecutive-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!