[–] [S] 2 points 1 year ago* (last edited 1 year ago)

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.)

  • source
  • parent
  • context
  • [–] 1 point 1 year ago

    Algorithmically, I think you'd want to use a binary search algorithm to find the index of the threshold value, or the index of where it should be. Since the value at that index is either the threshold or the first value greater than the threshold, you can check the value at that index and, if not equal to the threshold, subtract one from the index. Then it's a matter of making a subslice of the list of values starting at that index until the end of the list.

  • source
  • [–] 2 points 1 year ago (1 child)

    Still, I’m also wondering: Can awk or any other standard tool do something like “for deciding whether to print this line, look at the next line”?

    I don't know enough about awk to think this through to the end, but a trick from functional programming, which you might be able to apply here, is to 'zip' the list with itself, offset by one.

    So, it might then look like this, for example:

    140,141
    141,145
    145,180
    180,190
    190,
    

    Of course, in Bash you would probably need to implement the zipping yourself, so it might not save you much effort here...

  • source
  • [–] [S] 1 point 1 year ago

    I took sudo out of the examples and disabled it by default within the script. I still have a personal use for it.

    I keep a local backup on each device then transfer that backup to my desktop. Rsync requires root access to transfer files or directories with certain attributes over ssh. Otherwise the backup copy to my desktop is incomplete.

    Fortunately I already coded in a toggle for requesting root since Termux on Android has no root by default. I just won't note that in the readme file. That can be left for anyone who cares to read the code itself.

  • source
  • parent
  • context
  • [–] [S] 2 points 1 year ago*

    I use a lot of commands that either use the --delete option or require remote root access in order to preserve hard links and other attributes.

    I didn't know that was an issue. I was going from my own limited experience with linux.

    I already set an option to disable the root requirement at the beginning of the script. Simply changing the value to 0 will disable it and will let rsync display it's own errors.

    What exactly makes it suspect so I know what I'm doing?

  • source
  • parent
  • context
  • view more: ‹ prev