nmh-workers
[Top] [All Lists]

Re: check if message is in a particular sequence?

2021-05-02 18:39:47
I don't know about others, Ralph, but I would prefer you don't
shut up soon.  B-)  This is how I learn (or am reminded) about
all kinds of features of awk/sed/bash/etc. -- in addition to NMH
stuff I never knew since I'm such a "basic" user -- and I really
appreciate the (perhaps unintended) lessons!  B-)

                                Bob

From:     Ralph Corderoy <ralph@inputplus.co.uk>
To:       nmh-workers@nongnu.org
Date:     Sat, 01 May 2021 12:26:06 +0100
Subject:  Re: check if message is in a particular sequence?

Hi Bob,

Ah, thanks, Ralph!

Thanks to you for reminding me of awk's RS which, coupled with knowing
the only words starting with a positive number are a message range,
shortens my earlier sed and awk to

    $ mark -list -seq public -seq private -seq notexist
    public: 1-3 42
    private (private): 3141 97057-97059
    notexist: 
    $
    $ mark -list -seq public -seq private -seq notexist |
    > gawk -v RS=' |\n' -F - '
    >     $0+0 {u = NF==2 ? $2 : $1; for (n = $1; n <= u; n++) print n}
    > '
    1
    2
    3
    42
    3141
    97057
    97058
    97059
    $

-- 
Cheers, Ralph.



From:     Ralph Corderoy <ralph@inputplus.co.uk>
To:       nmh-workers@nongnu.org
Date:     Sat, 01 May 2021 12:43:27 +0100
Subject:  Re: check if message is in a particular sequence?

Hi,

I wrote:
    $ mark -list -seq public -seq private -seq notexist
    public: 1-3 42
    private (private): 3141 97057-97059
    notexist: 
    $
    $ mark -list -seq public -seq private -seq notexist |
    > gawk -v RS=' |\n' -F - '
    >     $0+0 {u = NF==2 ? $2 : $1; for (n = $1; n <= u; n++) print n}
    > '
    1
    2
    3
    42
    3141
    97057
    97058
    97059
    $

Silly me.  No need for gawk's regexp RS as the ‘42\nprivate’ which
arrives with just the POSIX RS=' ' always has something to discard after
the linefeed so there's no need to split it off into its own record.
Thus, the above simplifies further, with the coercing of $1, into

    mark -list -seq public -seq private -seq notexist |
    awk -v RS=' ' -F - '
      $0+0 {u = NF==2 ? $2 : $1; for (n = $1+0; n <= u; n++) print n}
    '

-- 
Cheers, Ralph.



From:     Ralph Corderoy <ralph@inputplus.co.uk>
To:       nmh-workers@nongnu.org
Date:     Sat, 01 May 2021 12:59:16 +0100
Subject:  Re: check if message is in a particular sequence?

I'll shut up soon.

    mark -list -seq public -seq private -seq notexist |
    awk -v RS=' ' -F - '
    $0+0 {u = NF==2 ? $2 : $1; for (n = $1+0; n <= u; n++) print n}
    '

I thought I may as well create this and see if it gets used.

    $ cat ~/bin/mhinseq
    #! /bin/sh

    # Successfully exit only if the message is in the sequence.
    # usage: mhinseq seq 42

    mark -list -sequence "${1?}" |
    awk -v RS=' ' -F - -v msg="${2?}" '
          $0+0 &&
              ((NF == 1 && $1+0 == msg) ||
               (NF == 2 && $1 <= msg && msg <= $2)) {f=1; last}
          END {exit !f}
      '
    $ 

-- 
Cheers, Ralph.

<Prev in Thread] Current Thread [Next in Thread>