Skip Navigation

InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)LO
Posts
11
Comments
7
Joined
4 wk. ago

  • For the first code snippet to run correctly, $list would need to be put in double quotes: echo "$list" | ... , because otherwise echo will conflate the various lines into a single line.

    The for loop approach is indeed quite readable. To make it solve the original task (which here means that it should also assign a number just smaller than $threshold to $tail, if $threshold is not itself contained in $list), one will have to do something in the spirit of what @Ephera@lemmy.ml and I describe in these comments.

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

  • Shell Scripting @programming.dev

    Say you have a list of increasing numbers and a threshold. How do you get the highest number smaller-or-equal to the threshold and all numbers that are larger?

  • Thanks, that's good to know, I'll see how well I can adapt my workflow to this. (The reason for using Konsole tabs so far is the easy switching via Alt+[number], but I suppose using Helix's integrated multi-document system should offer other advantages (e.g. regarding registers) that could outweigh this by far.)

  • Helix Editor @programming.dev

    How to make current filename show up as the window/console title? (Konsole being my terminal emulator)

  • Very specifically for learning about GNU/Linux and Unix, I highly recommend the book Classic Shell Scripting by Arnold Robbins and Nelson Beebe (O'Reilly Media, 2005).

    ISBN: 9780596005955

    I recently wrote the following about it in a post:

    This book is extremely readable and gives a very good introduction to the various standard Unix shell commands (grep, sed, awk, tr, sort, to name but a few) and how to tie them together to do useful things. It’s very suitable if you have some experience with the command line at the level of individual commands but now want to see how to do construct more interesting pipelines and scripts. It includes an introduction to regular expressions. The fact that the book is already 20 years certainly means that some explanations and approaches are outdated, but since shell programming is at the core about text processing, almost all contents of the book are still highly relevant today.

  • Libre Hardware @lemmy.ml

    The long, long road to a Free Printer

    Programming Books @programming.dev

    Classic Shell Scripting (O'Reilly Media, 2005). Great for leveling up one's skills with the Unix shell

    OSHW - Open Source Hardware @scribe.disroot.org

    Researchers launch open-source robotic exoskeleton to help people walk

    Open Source Hardware @lemmy.ml

    Researchers launch open-source robotic exoskeleton to help people walk

    Libre Hardware @lemmy.ml

    Researchers launch open-source robotic exoskeleton to help people walk

    Web Development @programming.dev

    Things I would like every young web engineer to learn (Toot by Alex Russell)

    Hardware @programming.dev

    Reviving the Spirit of Open Science Hardware: Highlights from the 2025 Open Hardware Summit

    OSHW - Open Source Hardware @scribe.disroot.org

    Reviving the Spirit of Open Science Hardware: Highlights from the 2025 Open Hardware Summit

    Libre Hardware @lemmy.ml

    Reviving the Spirit of Open Science Hardware: Highlights from the 2025 Open Hardware Summit