Subtracting numbers from adjacent columns and successive rows using awk

awk

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?

Best Answer

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
Related Question