Subtract with previous index where index is not a number using awk
1
vote
2
answers
285
views
i would like to subtract values from previous values but a condition must be satisfied.
File:
2022-02-01 01:00:00|abc|2001|5
2022-02-01 01:00:00|abc|2002|10
2022-02-01 01:01:00|abc|2001|6
2022-02-01 01:01:00|abc|2002|11
2022-02-01 01:02:00|abc|2001|10
2022-02-01 01:02:00|abc|2002|11
2022-02-01 01:02:00|abc|2003|2
Desired output:
2022-02-01 01:00:00|abc|2001|5
2022-02-01 01:00:00|abc|2002|10
2022-02-01 01:01:00|abc|2001|1
2022-02-01 01:01:00|abc|2002|1
2022-02-01 01:02:00|abc|2001|4
2022-02-01 01:02:00|abc|2002|0
2022-02-01 01:02:00|abc|2003|2
I tried with awk but couldnt reach the previous index to subtract the value
awk -F"|" 'BEGIN{OFS="|"}{a[$1,$2,$3]=$4}END{for (i in a) print i, a[i]-a[i-1] }' file
Asked by ammar
(87 rep)
Oct 26, 2022, 08:26 PM
Last activity: Oct 29, 2022, 07:01 PM
Last activity: Oct 29, 2022, 07:01 PM