Script to subtract rows HELP

Dear All,

Please help with a script which can accomplish the following:

Input table:

$1 $2 $3

Student1 1 50
Student2 56 75
Student3 77 100

Desired Output:

$1 $2 $3 $4

Student1 1 50
Student2 56 75 6
Student3 77 100 2

i want to subtract 56 ($2) - 50($3) = 6 ($4); 77($2) - 75($3) = 2 ($4)and print the difference as $4. i have a list running 1000s of rows.

Please help me to solve this.

try this,

awk '{if(NR==1){a=$3;print}else {print $0,$2-a;a=$3}}' inputfile
1 Like

Thanks, it works!

awk 'x{$0=$0FS ($2-x)}{x=$3}1' file
1 Like

This helped me today. Thank you pravin27 :slight_smile: