• 5 Posts
  • 3 Comments
Joined 25 days ago
cake
Cake day: June 13th, 2025

help-circle


  • Thank you, in fact I ended up doing something that’s mathematically pretty much just that: I have the previous line stored in an auxiliary variable lastline, and it is the evaluation of the current line $0 that determines whether the previous line gets printed.

    awk -v threshold=150 'BEGIN {lastline=""}
      (lastline!="" && threshold<$0){print lastline} #the additional check lastline!="" prevents an empty line at the very beginning
      {lastline=$0}
      END{print} #hardcode printing of the very last line, because otherwise it would never be printed
    ' 
    

    Of note, in the case where some list entries are repeated, the behavior of this script will be:

    • The threshold value, if it’s in the list, will always be printed just once, even if it occurs multiple times in the list, and also if it happens to be the first, last, or only entry in the list.
    • All larger entries will be printed exactly as often as they occur in the list. This even holds for the largest value: its last repetition will be printed via the final END{print} statement, whereas all preceding instances get printed through the statement that depends on threshold<$0.

    (IIRC, it was a StackOverflow post that led me to this.)