5

I have a tab-separated file that looks like this:

NZ_CP023599.1 WP_003911075.1 302845 305406 NZ_CP023599.1 WP_003898428.1 471171 472583 NZ_CP023599.1 WP_003402248.1 534387 535157 NZ_CP023599.1 WP_003402301.1 552556 553950 NZ_CP023599.1 WP_003402318.1 558837 559697 

I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.

The output would look like this:

NZ_CP023599.1 WP_003911075.1 302845 305406 NZ_CP023599.1 WP_003898428.1 471171 472583 165765 NZ_CP023599.1 WP_003402248.1 534387 535157 61804 NZ_CP023599.1 WP_003402301.1 552556 553950 17399 NZ_CP023599.1 WP_003402318.1 558837 559697 4887 

How do I go about this using awk?

2 Answers 2

6

You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.

awk -F'\t' 'BEGIN { OFS = FS } NR == 1 { last = $4; print; next }{ $5 = $3 - last; last = $4 }1' file 
0
0
awk -F\\t '{ if (length(prev4)>0) { col5 = FS ($3-prev4) }; print $0 col5; prev4 = $4 }' InputFile 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.