问题
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