Rahman Afzal
Rahman Afzal

Reputation: 11

difference between number in the same column using AWK command

I have a file containing one column of number:

1
2
4
7
10
12

and i want output like below

1
3
2

e.g I need difference of each two iteration.

like 2-1=1 , 7-4=3, 12-10=2

Upvotes: 0

Views: 54

Answers (1)

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

awk 'NR%2{x=$0;next}{print $0-x}' File

For the odd lines, save the line (number) to variable x. For even lines, print the difference using previously saved x

Upvotes: 1

Related Questions