Skip to content

Instantly share code, notes, and snippets.

@cmj
Last active July 15, 2024 10:12
Show Gist options
  • Save cmj/5027041575ec1aee5c50bbf71224730e to your computer and use it in GitHub Desktop.
Save cmj/5027041575ec1aee5c50bbf71224730e to your computer and use it in GitHub Desktop.
Examples on subtracting number from previous line.

Input file:

$ tail stats.dat
60316
60316
60316
60316
60316
60316
60318
60318
60320
60320
$ awk 'NR>1{print$0-prev}{prev=$0}' stats.dat
0
0
0
0
0
0
2
0
2
0

Input file with multiple columns:

$ tail twitter.dat
20240714-192001 tweets: 47059 friends: 656 followers: 189760888 likes: 60323
20240714-192501 tweets: 47059 friends: 656 followers: 189761528 likes: 60323
20240714-193001 tweets: 47059 friends: 656 followers: 189762223 likes: 60323
20240714-193501 tweets: 47059 friends: 656 followers: 189762920 likes: 60323
20240714-194001 tweets: 47059 friends: 656 followers: 189763516 likes: 60323
20240714-194501 tweets: 47059 friends: 656 followers: 189764234 likes: 60323
20240714-195001 tweets: 47060 friends: 656 followers: 189764877 likes: 60324
20240714-195501 tweets: 47061 friends: 656 followers: 189765523 likes: 60324
20240714-200001 tweets: 47063 friends: 656 followers: 189766075 likes: 60328
20240714-200501 tweets: 47064 friends: 656 followers: 189766640 likes: 60330
$ paste -d- <(tail -n+2 twitter.dat | cut -d\  -f9) <(head -n-1 twitter.dat | cut -d\  -f9) | tail | bc
3
0
0
0
0
0
1
0
4
2

Adding 2 columns:

paste <(tail -n+2 twitter.dat | cut -d\  -f1,3,9) <(head -n-1 twitter.dat | cut -d\  -f1,3,9) |
  while read ts_1 tw_1 li_1 ts_0 tw_0 li_0; do
    counter=$(($li_1-$li_0+$tw_1-$tw_0))
    echo "${ts_1:: -2} $counter"
  done | tail
  
20240714-1920 7
20240714-1925 0
20240714-1930 0
20240714-1935 0
20240714-1940 0
20240714-1945 0
20240714-1950 2
20240714-1955 1
20240714-2000 6
20240714-2005 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment