• Re: How do I escape a string that contains both single and double quote

    From Janis Papanagnou@21:1/5 to Ottavio Caruso on Thu Mar 17 11:41:20 2022
    On 17.03.2022 11:16, Ottavio Caruso wrote:
    I have a function that checks the occurrence of characters in a string:

    cw-sort ()
    {
    echo $1 | sed 's/./&\n/g' | sort | uniq -ic | sort -rn
    }


    This is all fine and dandy as long as there are no funny characters:

    $ cw-sort uyrwqoequwyroqiuwey8378429537uriewr
    [...]
    When you throw single quotes in the mix, you have to escape them in
    double quotes:

    $ cw-sort "uyrwqoequwyroqiuwey83'''''78429537uriewr"
    [...]
    Likewise, if you throw double quotes in the mix, you have to escape them
    in single quotes:

    $ cw-sort 'uyrwqoequwyroqiuwey83"""""""""""78429537uriewr'
    [...]

    But what if you have both single and double quotes?

    $ cw-sort 'uyrwqoequwyroqiuwey83""""'''''78429537uriewr'

    $
    $ cw-sort "uyrwqoequwyroqiuwey83""""'''''78429537uriewr"
    [...]
    Wrapping the string in single quotes, makes the function choke. Wrapping
    the string in double quotes makes the double quotes disappear from the
    count.

    How do I get out of this?

    $ echo '"double"'and"'single'"
    "double"and'single'

    (likewise the word /and/ can be put inside the double or single quoted substrings).

    Got the trick (or is an explanation necessary)?

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to Ottavio Caruso on Thu Mar 17 09:06:16 2022
    On 3/17/2022 5:16 AM, Ottavio Caruso wrote:
    Hi,


    I have a function that checks the occurrence of characters in a string:

    cw-sort ()
    {
    echo $1 | sed 's/./&\n/g' | sort | uniq -ic | sort -rn
    }

    Always quote your variables (see https://mywiki.wooledge.org/Quotes)
    unless you have a specific need to not do so, which you don't, (try
    without quotes if your input string was `*` or otherwise matched files
    in your directory) and use printf instead of echo for robustness and portability . Also the above echo+sed will add a presumably undesirable
    newline to the list of chars seen by sort (note the standalone `1` at
    the end of your posted output) so for portability and robustness the
    above should really be:

    cw-sort () {
    printf '%s\n' "$1" | grep -o. | sort | uniq -ic | sort -rn
    }

    If your grep version doesn't have a `-o` option then you could instead
    use either of:

    printf ... | fold -w1 | sort ...
    printf ... | awk '{for (i=1;i<=length();i++) print substr($0,i,1)}'
    | sort

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    <snip>
    $ cw-sort "uyrwqoequwyroqiuwey83""""'''''78429537uriewr"
    5 '
    4 w
    4 u
    4 r
    3 y
    3 q
    3 e
    2 o
    2 i
    2 8
    2 7
    2 3
    1 9
    1 5
    1 4
    1 2
    1

    Wrapping the string in single quotes, makes the function choke. Wrapping
    the string in double quotes makes the double quotes disappear from the count.

    How do I get out of this?


    In your mind split:

    uyrwqoequwyroqiuwey83""""'''''78429537uriewr

    into segments with each type of quote:

    uyrwqoequwyroqiuwey83"""" ''''' 78429537uriewr

    then quote those segments as you usually do:

    'uyrwqoequwyroqiuwey83""""' "'''''" '78429537uriewr'

    then glue them back together to get:

    'uyrwqoequwyroqiuwey83""""'"'''''"'78429537uriewr'

    e.g.:

    $ cw-sort 'uyrwqoequwyroqiuwey83""""'"'''''"'78429537uriewr'
    5 '
    4 w
    4 u
    4 r
    4 "
    3 y
    3 q
    3 e
    2 o
    2 i
    2 8
    2 7
    2 3
    1 9
    1 5
    1 4
    1 2

    Regards,

    Ed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Ottavio Caruso on Thu Mar 17 12:59:59 2022
    On 2022-03-17, Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:

    How do I escape a string that contains both single and double quotes?

    You can put part of the string in single quotes, part in double
    quotes...

    "aaaa"'bbbb'

    ... and now with the respective other quote character inserted:

    "aa'aa"'bb"bb'

    Or you can use the backslash to escape individual quotes inside
    double quotes:

    "aa\"bb"

    Or you can close the single quotes, escape an individual single
    quote with a backslash, and open the single quotes again:

    'aa'\''bb'

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to Ed Morton on Thu Mar 17 09:17:39 2022
    On 3/17/2022 9:06 AM, Ed Morton wrote:
    On 3/17/2022 5:16 AM, Ottavio Caruso wrote:
    Hi,


    I have a function that checks the occurrence of characters in a string:

    cw-sort ()
    {
          echo $1 | sed 's/./&\n/g' | sort | uniq -ic | sort -rn
    }

    Always quote your variables (see https://mywiki.wooledge.org/Quotes)
    unless you have a specific need to not do so, which you don't, (try
    without quotes if your input string was `*` or otherwise matched files
    in your directory) and use printf instead of echo for robustness and portability . Also the above echo+sed will add a presumably undesirable newline to the list of chars seen by sort (note the standalone `1` at
    the end of your posted output) so for portability and robustness the
    above should really be:

        cw-sort () {
            printf '%s\n' "$1" | grep -o. | sort | uniq -ic | sort -rn
        }

    If your grep version doesn't have a `-o` option then you could instead
    use either of:

        printf ... | fold -w1 | sort ...
        printf ... | awk '{for (i=1;i<=length();i++) print substr($0,i,1)}'
    | sort

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    <snip>
    $ cw-sort "uyrwqoequwyroqiuwey83""""'''''78429537uriewr"
            5 '
            4 w
            4 u
            4 r
            3 y
            3 q
            3 e
            2 o
            2 i
            2 8
            2 7
            2 3
            1 9
            1 5
            1 4
            1 2
            1

    Wrapping the string in single quotes, makes the function choke. Wrapping the string in double quotes makes the double quotes disappear from the count.

    How do I get out of this?


    In your mind split:

        uyrwqoequwyroqiuwey83""""'''''78429537uriewr

    into segments with each type of quote:

    To clarify that, I really meant separate the segments with single quotes
    from those without since you should always wrap strings in single quotes
    unless they NEED double quotes (e.g. if they themselves contain single
    quotes) so you end up with the segments with single quotes inside double quotes, and the rest within single quotes.


        uyrwqoequwyroqiuwey83""""   '''''   78429537uriewr

    then quote those segments as you usually do:

        'uyrwqoequwyroqiuwey83""""'   "'''''"   '78429537uriewr'

    then glue them back together to get:

        'uyrwqoequwyroqiuwey83""""'"'''''"'78429537uriewr'

    e.g.:

        $ cw-sort 'uyrwqoequwyroqiuwey83""""'"'''''"'78429537uriewr'
              5 '
              4 w
              4 u
              4 r
              4 "
              3 y
              3 q
              3 e
              2 o
              2 i
              2 8
              2 7
              2 3
              1 9
              1 5
              1 4
              1 2

    Regards,

        Ed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to Janis Papanagnou on Thu Mar 17 09:58:31 2022
    On 3/17/2022 9:19 AM, Janis Papanagnou wrote:
    On 17.03.2022 15:06, Ed Morton wrote:

    printf ... | fold -w1 | sort ...
    [...]

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    The fold command is at least as old as UNIX Release 7, so it would
    be surprising if it weren't specified in POSIX (where you can indeed
    find it).

    It's specified by POSIX, I just don't know if it's *mandatory* by POSIX.

    Ed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Ed Morton on Thu Mar 17 15:19:40 2022
    On 17.03.2022 15:06, Ed Morton wrote:

    printf ... | fold -w1 | sort ...
    [...]

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    The fold command is at least as old as UNIX Release 7, so it would
    be surprising if it weren't specified in POSIX (where you can indeed
    find it).

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Ed Morton on Thu Mar 17 16:24:08 2022
    On 17.03.2022 15:58, Ed Morton wrote:
    On 3/17/2022 9:19 AM, Janis Papanagnou wrote:
    On 17.03.2022 15:06, Ed Morton wrote:

    printf ... | fold -w1 | sort ...
    [...]

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    The fold command is at least as old as UNIX Release 7, so it would
    be surprising if it weren't specified in POSIX (where you can indeed
    find it).

    It's specified by POSIX, I just don't know if it's *mandatory* by POSIX.

    And where would that distinction be made? (I can't see anything.) https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fold.html

    And what would be the rationale for not declaring such a basic tool
    as mandatory that is around in the Unix toolbox for more than four
    decades?

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to Janis Papanagnou on Thu Mar 17 11:49:29 2022
    On 3/17/2022 10:24 AM, Janis Papanagnou wrote:
    On 17.03.2022 15:58, Ed Morton wrote:
    On 3/17/2022 9:19 AM, Janis Papanagnou wrote:
    On 17.03.2022 15:06, Ed Morton wrote:

    printf ... | fold -w1 | sort ...
    [...]

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    The fold command is at least as old as UNIX Release 7, so it would
    be surprising if it weren't specified in POSIX (where you can indeed
    find it).

    It's specified by POSIX, I just don't know if it's *mandatory* by POSIX.

    And where would that distinction be made? (I can't see anything.) https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fold.html

    The mandatory ones are those not marked as optional in their definition
    page. It'd be great if there was just a list of such tools but AFAIK
    there isn't.

    And what would be the rationale for not declaring such a basic tool
    as mandatory that is around in the Unix toolbox for more than four
    decades?

    You'd have to ask the guys who write the specs but "od" is one example
    of a basic tool that's been around for decades and apparently isn't
    mandatory since it's annotated with "XSI" in the spec - https://pubs.opengroup.org/onlinepubs/9699919799/utilities/od.html.
    Similarly for cal, ex, talk, who, yacc, etc. - they have various
    annotations and so are not mandatory.

    You inspired me to look and I see no annotation for "fold" at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fold.html so
    it must be mandatory.

    Ed

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Janis Papanagnou on Thu Mar 17 17:33:09 2022
    Janis Papanagnou <janis_papanagnou@hotmail.com> writes:

    On 17.03.2022 15:06, Ed Morton wrote:

    printf ... | fold -w1 | sort ...
    [...]

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    The fold command is at least as old as UNIX Release 7, so it would
    be surprising if it weren't specified in POSIX (where you can indeed
    find it).

    Is Release 7 the same as version 7 (i.e. the first widely distributed
    version from the late 70s)? If so, I can't find any record of fold being
    part of it.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Janis Papanagnou on Thu Mar 17 22:09:48 2022
    On 2022-03-17, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    The fold command is at least as old as UNIX Release 7, so it would
    be surprising if it weren't specified in POSIX (where you can indeed
    find it).

    fold(1) seems to be from BSD. Bill Joy wrote it on June 28, 1977.
    (Says the first version checked into the CSRG repository in 1980.)

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to Janis Papanagnou on Thu Mar 17 20:00:04 2022
    On Thu, 17 Mar 2022 19:28:52 -0400, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
    Yes, I meant Version 7. The source I looked at is my first (German)
    book about Unix; it is based on Version 7 and was published 1984.
    There the fold command syntax is defined as fold [-width] {files}

    https://www.gnu.org/software/coreutils/manual/coreutils.html#fold-invocation

    The -width option is considered obsolete.

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Ben Bacarisse on Fri Mar 18 00:28:52 2022
    On 17.03.2022 18:33, Ben Bacarisse wrote:
    Janis Papanagnou <janis_papanagnou@hotmail.com> writes:

    On 17.03.2022 15:06, Ed Morton wrote:

    printf ... | fold -w1 | sort ...
    [...]

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    The fold command is at least as old as UNIX Release 7, so it would
    be surprising if it weren't specified in POSIX (where you can indeed
    find it).

    Is Release 7 the same as version 7 (i.e. the first widely distributed
    version from the late 70s)? If so, I can't find any record of fold being part of it.

    Yes, I meant Version 7. The source I looked at is my first (German)
    book about Unix; it is based on Version 7 and was published 1984.
    There the fold command syntax is defined as fold [-width] {files}

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to David W. Hodgins on Fri Mar 18 01:11:10 2022
    On 18.03.2022 01:00, David W. Hodgins wrote:
    On Thu, 17 Mar 2022 19:28:52 -0400, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
    Yes, I meant Version 7. The source I looked at is my first (German)
    book about Unix; it is based on Version 7 and was published 1984.
    There the fold command syntax is defined as fold [-width] {files}

    https://www.gnu.org/software/coreutils/manual/coreutils.html#fold-invocation


    The -width option is considered obsolete.

    I am aware of that. I mentioned the old syntax to show that (while
    fold was existing in Version 7) there's a difference in how it can
    be used; the "old" 'fold' did also not support -s, and -b (that is
    certainly necessary nowadays to distinguish bytes from characters).

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to David W. Hodgins on Thu Mar 17 23:06:19 2022
    "David W. Hodgins" <dwhodgins@nomail.afraid.org> writes:
    On Thu, 17 Mar 2022 19:28:52 -0400, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
    Yes, I meant Version 7. The source I looked at is my first (German)
    book about Unix; it is based on Version 7 and was published 1984.
    There the fold command syntax is defined as fold [-width] {files}

    https://www.gnu.org/software/coreutils/manual/coreutils.html#fold-invocation

    The -width option is considered obsolete.

    Just in case it isn't clear, the obsolete "-width" option means, for
    example:
    fold -72

    The coreutils version continues to support that, but recommends
    fold -w 72
    or
    fold --width=72

    POSIX specifies "fold -w 72" and doesn't mention the "fold -72" form.

    (For arbitrary values of 72, of course.)

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    Working, but not speaking, for Philips
    void Void(void) { Void(); } /* The recursive call of the void */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Jorgen Grahn on Fri Mar 18 10:05:01 2022
    On 18.03.2022 09:28, Jorgen Grahn wrote:
    On Thu, 2022-03-17, Ottavio Caruso wrote:
    Hi,


    I have a function that checks the occurrence of characters in a string:

    cw-sort ()
    {
    echo $1 | sed 's/./&\n/g' | sort | uniq -ic | sort -rn
    }

    (Nitpick: the name of the function and the description don't fit. The
    sorting seems incidental rather than essential.)

    This is all fine and dandy as long as there are no funny characters:

    $ cw-sort uyrwqoequwyroqiuwey8378429537uriewr

    I think your problem stems from putting data in variables instead of
    in pipes/streams.

    I don't think so. Whether the stream or variable, at that point where
    the value is assigned it needs proper quoting. Here is a function that
    accepts both methods, and input measures and the results are the same.

    cw_sort ()
    {
    if (($#))
    then printf "%s" "$1"
    else cat -
    fi | sed 's/./&\n/g' | sort | uniq -ic | sort -rn
    }

    echo ===
    cw_sort "gsd'u'z'dfed"'gw"ef"d"ev"djnfr'
    echo ===
    printf "%s" "gsd'u'z'dfed"'gw"ef"d"ev"djnfr' | cw_sort
    echo ===

    Notes: For simplicity I used non-standard ((...)). And I changed the
    '-' in the function name to '_' to be more portable.

    Janis

    The very first thing your function does is (via
    echo) transforming the input back to a stream, so the function itself
    doesn't require that.

    If your cw-sort function read from stdin instead, could you still fit
    it into your workflow? I'm assuming you're normally calling it from
    some larger script.

    /Jorgen


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jorgen Grahn@21:1/5 to Ottavio Caruso on Fri Mar 18 08:28:18 2022
    On Thu, 2022-03-17, Ottavio Caruso wrote:
    Hi,


    I have a function that checks the occurrence of characters in a string:

    cw-sort ()
    {
    echo $1 | sed 's/./&\n/g' | sort | uniq -ic | sort -rn
    }

    (Nitpick: the name of the function and the description don't fit. The
    sorting seems incidental rather than essential.)

    This is all fine and dandy as long as there are no funny characters:

    $ cw-sort uyrwqoequwyroqiuwey8378429537uriewr

    I think your problem stems from putting data in variables instead of
    in pipes/streams. The very first thing your function does is (via
    echo) transforming the input back to a stream, so the function itself
    doesn't require that.

    If your cw-sort function read from stdin instead, could you still fit
    it into your workflow? I'm assuming you're normally calling it from
    some larger script.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ottavio Caruso@21:1/5 to Jorgen Grahn on Fri Mar 18 11:32:42 2022
    On 18/03/2022 08:28, Jorgen Grahn wrote:
    If your cw-sort function read from stdin instead, could you still fit
    it into your workflow? I'm assuming you're normally calling it from
    some larger script.

    No, I call it from the shell.

    This is supposed to check the frequency of mistakes I make while
    learning Morse code on lcwo.net.

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Janis Papanagnou on Fri Mar 18 15:51:01 2022
    On 2022-03-17, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    Is Release 7 the same as version 7 (i.e. the first widely distributed
    version from the late 70s)? If so, I can't find any record of fold being
    part of it.

    Yes, I meant Version 7. The source I looked at is my first (German)
    book about Unix; it is based on Version 7 and was published 1984.
    There the fold command syntax is defined as fold [-width] {files}

    I don't think that book is a reliable source in this respect.

    You can browse the source tree of UNIX V7 here: https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7

    There is no fold(1) command.

    fold.c did however ship with 1BSD. (It's in s6/cont.a.)

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Christian Weisgerber on Fri Mar 18 23:41:21 2022
    On 18.03.2022 16:51, Christian Weisgerber wrote:
    On 2022-03-17, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    Is Release 7 the same as version 7 (i.e. the first widely distributed
    version from the late 70s)? If so, I can't find any record of fold being >>> part of it.

    Yes, I meant Version 7. The source I looked at is my first (German)
    book about Unix; it is based on Version 7 and was published 1984.
    There the fold command syntax is defined as fold [-width] {files}

    I don't think that book is a reliable source in this respect.

    That may be, I really cannot tell. But reinspecting the introductory
    chapter of that book more closely I saw that it says it's based on
    UNIX Version 7 and that it also includes the description of a couple
    common and wide spread extensions. So that would be in sync with your observation WRT Version 7. But note that my comment was not so much
    focused on which exact version it was but since when the fold command
    was known; and since the book was from 1984 fold is now already known
    about four decades. But thanks for catching that inaccuracy concerning
    the mentioned release!

    You can browse the source tree of UNIX V7 here: https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7

    It seems there's a lot tools missing at that time (like cut).


    There is no fold(1) command.

    fold.c did however ship with 1BSD. (It's in s6/cont.a.)

    When was that? (Must have been before 1984, I'd have to suppose.)

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Janis Papanagnou on Fri Mar 18 22:58:09 2022
    Janis Papanagnou <janis_papanagnou@hotmail.com> writes:

    On 17.03.2022 18:33, Ben Bacarisse wrote:
    Janis Papanagnou <janis_papanagnou@hotmail.com> writes:

    On 17.03.2022 15:06, Ed Morton wrote:

    printf ... | fold -w1 | sort ...
    [...]

    AFAIK fold isn't a mandatory POSIX tool but awk is and so WILL be
    available on your system.

    The fold command is at least as old as UNIX Release 7, so it would
    be surprising if it weren't specified in POSIX (where you can indeed
    find it).

    Is Release 7 the same as version 7 (i.e. the first widely distributed
    version from the late 70s)? If so, I can't find any record of fold being
    part of it.

    Yes, I meant Version 7. The source I looked at is my first (German)
    book about Unix; it is based on Version 7 and was published 1984.
    There the fold command syntax is defined as fold [-width] {files}

    There's no direct evidence: a VM of V7 Unix I have has no fold command,
    the online archives of V7 have no fold command, and (least reliable of
    all) I don't remember a fold command in V7!

    (It's just an academic point -- it's in POSIX and can be relied on.)

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Waitzmann@21:1/5 to All on Fri Mar 18 21:58:42 2022
    Janis Papanagnou <janis_papanagnou@hotmail.com>:

    if (($#))

    […]

    Notes: For simplicity I used non-standard ((...)).


    If one wants to do it the standard way one can do


    if ${1+:} false

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Helmut Waitzmann on Sat Mar 19 00:21:41 2022
    On 18.03.2022 21:58, Helmut Waitzmann wrote:
    Janis Papanagnou <janis_papanagnou@hotmail.com>:

    if (($#))

    […]

    Notes: For simplicity I used non-standard ((...)).

    If one wants to do it the standard way one can do

    if ${1+:} false

    An interesting construct.

    I'd have more thought of the conventional (and likely
    easier to understand) if [ $# -gt 0 ]

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Wayne@21:1/5 to Christian Weisgerber on Sat Mar 19 04:13:58 2022
    On 3/18/2022 11:51 AM, Christian Weisgerber wrote:
    On 2022-03-17, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    ...
    fold.c did however ship with 1BSD. (It's in s6/cont.a.)


    Is that the same fold command though? It's been a very long time
    but I still remember the original fold command inserted DEL bytes
    every 6 inches, to allow folding of the paper tape from an ASR-33!
    I don't know if that was the fold.c in BSD though.

    --
    Wayne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Wayne on Sat Mar 19 13:09:56 2022
    On 2022-03-19, Wayne <wayne@nospam.invalid> wrote:

    fold.c did however ship with 1BSD. (It's in s6/cont.a.)

    Is that the same fold command though?

    Yes.

    Here's the initial version from the Git conversion of the CSRG's
    SCCS repository: https://github.com/jonathangray/csrg/commit/f4a92b564ec31c3fae61558c8fbcd913cace1ad1

    It didn't really change until Kevin Ruddy rewrote it in 1990.

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Janis Papanagnou on Sat Mar 19 13:00:03 2022
    On 2022-03-18, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    There is no fold(1) command.

    fold.c did however ship with 1BSD. (It's in s6/cont.a.)

    When was that? (Must have been before 1984, I'd have to suppose.)

    Late 1977/early 1978.

    "The UNIX system family tree: Research and BSD" https://cgit.freebsd.org/src/tree/share/misc/bsd-family-tree
    (Scroll down a few hundred lines, there's more after the spaghetti
    graphic.)

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jorgen Grahn@21:1/5 to Ottavio Caruso on Sat Mar 19 15:40:53 2022
    On Fri, 2022-03-18, Ottavio Caruso wrote:
    On 18/03/2022 08:28, Jorgen Grahn wrote:
    If your cw-sort function read from stdin instead, could you still fit
    it into your workflow? I'm assuming you're normally calling it from
    some larger script.

    No, I call it from the shell.

    This is supposed to check the frequency of mistakes I make while
    learning Morse code on lcwo.net.

    So, do you then paste the input from some web page? In that case I'd personally prefer:

    % cw-sort
    *paste*
    <EOF>

    to

    % cw-sort *paste*

    followed by manual escaping and quoting.

    But oh, maybe that's what you were asking for: a simple one-size-
    fits-all way you can do that without thinking. I have always assumed
    there is no such thing, so I didn't even read the responses which went
    into quoting.

    If there was such a thing, I would consider using it.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Ed Morton on Sat Mar 19 17:51:20 2022
    On 17.03.2022 15:06, Ed Morton wrote:
    [...]

    If your grep version doesn't have a `-o` option then you could instead
    use either of:

    printf ... | fold -w1 | sort ...

    I just noticed that my fold command doesn't respect the locale settings.

    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    S

    �
    g
    e
    $ echo Säge | LC_ALL=C fold -w 1
    S

    �
    g
    e

    In a de_DE.UTF-8 locale the umlauts aren't handled properly, which makes
    it pretty useless. :-(

    Or is my fold version too old?

    $ fold --version
    fold (GNU coreutils) 8.13


    printf ... | awk '{for (i=1;i<=length();i++) print substr($0,i,1)}'
    | sort

    BTW, with GNU awk, setting FS="" you can use $i instead of substr(...).

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to Janis Papanagnou on Sat Mar 19 13:56:01 2022
    On Sat, 19 Mar 2022 12:51:20 -0400, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    I just noticed that my fold command doesn't respect the locale settings.

    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    S

    �
    g
    e
    $ echo Säge | LC_ALL=C fold -w 1
    S

    �
    g
    e

    In a de_DE.UTF-8 locale the umlauts aren't handled properly, which makes
    it pretty useless. :-(

    Or is my fold version too old?

    $ fold --version
    fold (GNU coreutils) 8.13

    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    S
    ä
    g
    e
    $ fold --version
    fold (GNU coreutils) 8.32
    Copyright (C) 2020 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Written by David MacKenzie.

    I'm using Mageia 8 with all of it's locale packages installed, and most of it's fonts.

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to Janis Papanagnou on Sat Mar 19 16:59:06 2022
    On 3/19/2022 11:51 AM, Janis Papanagnou wrote:
    On 17.03.2022 15:06, Ed Morton wrote:
    [...]

    If your grep version doesn't have a `-o` option then you could instead
    use either of:

    printf ... | fold -w1 | sort ...

    I just noticed that my fold command doesn't respect the locale settings.

    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    S

    �
    g
    e
    $ echo Säge | LC_ALL=C fold -w 1
    S

    �
    g
    e

    In a de_DE.UTF-8 locale the umlauts aren't handled properly, which makes
    it pretty useless. :-(

    Or is my fold version too old?

    No idea.

    $ fold --version
    fold (GNU coreutils) 8.13


    printf ... | awk '{for (i=1;i<=length();i++) print substr($0,i,1)}'
    | sort

    BTW, with GNU awk, setting FS="" you can use $i instead of substr(...).

    I know, I just wanted to provide a final "if all else fails" option
    that'd work anywhere and setting FS="" is undefined behavior per POSIX.

    Ed.

    Janis


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to David W. Hodgins on Sun Mar 20 02:46:46 2022
    On 19.03.2022 18:56, David W. Hodgins wrote:
    On Sat, 19 Mar 2022 12:51:20 -0400, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    I just noticed that my fold command doesn't respect the locale settings.

    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    [...]
    In a de_DE.UTF-8 locale the umlauts aren't handled properly, which makes
    it pretty useless. :-(

    Or is my fold version too old?

    $ fold --version
    fold (GNU coreutils) 8.13

    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    S
    ä
    g
    e
    $ fold --version
    fold (GNU coreutils) 8.32
    [...]

    Ah, fine, so that seems to have gotten fixed. Thanks!

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Eder@21:1/5 to Janis Papanagnou on Sun Mar 20 09:42:32 2022
    On So 20 Mär 2022 at 02:46, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    On 19.03.2022 18:56, David W. Hodgins wrote:
    On Sat, 19 Mar 2022 12:51:20 -0400, Janis Papanagnou
    <janis_papanagnou@hotmail.com> wrote:

    I just noticed that my fold command doesn't respect the locale settings. >>>
    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    [...]
    In a de_DE.UTF-8 locale the umlauts aren't handled properly, which makes >>> it pretty useless. :-(

    Or is my fold version too old?

    $ fold --version
    fold (GNU coreutils) 8.13

    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    S
    ä
    g
    e
    $ fold --version
    fold (GNU coreutils) 8.32
    [...]

    Ah, fine, so that seems to have gotten fixed. Thanks!

    Janis

    Hmm, here I have
    $ fold --version
    fold (GNU coreutils) 9.0
    [...]

    but
    $ echo Säge | LC_ALL=de_DE.UTF-8 fold -w 1
    S
    �

    g
    e

    'Andreas

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to Ben Bacarisse on Sun Mar 20 13:37:41 2022
    On Sun, 20 Mar 2022 00:20:29 -0400, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
    Hmm... I have 8.32 but

    $ echo Säge | LC_ALL=en_GB.UTF-8 fold -w 1
    S
    �
    �
    g
    e
    $ echo Säge | LC_ALL=en_GB.UTF-8 wc -m
    5
    $

    (That last one just to show that some utilities do get UTF-8 characters right).

    I suspect it's due to a missing locale or (more likely) font package.

    $ rpm -qa|grep locales|sort|wc -l
    185
    $ rpm -qa|grep font|sort|wc -l
    391

    $ echo Säge | LC_ALL=en_GB.UTF-8 fold -w 1
    S
    ä
    g
    e

    $ du -s /usr/share/fonts/
    3.0G /usr/share/fonts/

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to David W. Hodgins on Sun Mar 20 13:43:28 2022
    On Sun, 20 Mar 2022 13:37:41 -0400, David W. Hodgins <dwhodgins@nomail.afraid.org> wrote:
    I suspect it's due to a missing locale or (more likely) font package.
    $ echo Säge | LC_ALL=en_GB.UTF-8 fold -w 1
    S
    ä
    g
    e

    As it's bash in a terminal, is the terminus-font package installed?

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to Ben Bacarisse on Sun Mar 20 16:47:06 2022
    On Sun, 20 Mar 2022 16:30:00 -0400, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
    I don't see how it could be a font issue. The character displays fine,
    and even if it did not, that should have no effect on whether fold
    counts them correctly.

    Looking at the source of fold in coreutils 8.32 I can't see any code
    that could possibly count the UTF-8 encoding of ä as a single character.

    Are you using a single-byte character encoding? (Setting
    LC_ALL=en_GB.UTF-8 won't change that since fold appears to ignore the encoding.) What does echo Säge | hd show?

    $ man echo
    [dave@x3 ~]$ echo -n Säge|hexdump -x
    0000000 c353 67a4 0065
    0000005

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to David W. Hodgins on Sun Mar 20 20:30:00 2022
    "David W. Hodgins" <dwhodgins@nomail.afraid.org> writes:

    On Sun, 20 Mar 2022 13:37:41 -0400, David W. Hodgins <dwhodgins@nomail.afraid.org> wrote:
    I suspect it's due to a missing locale or (more likely) font package.
    $ echo Säge | LC_ALL=en_GB.UTF-8 fold -w 1
    S
    ä
    g
    e

    As it's bash in a terminal, is the terminus-font package installed?

    I don't see how it could be a font issue. The character displays fine,
    and even if it did not, that should have no effect on whether fold
    counts them correctly.

    Looking at the source of fold in coreutils 8.32 I can't see any code
    that could possibly count the UTF-8 encoding of ä as a single character.

    Are you using a single-byte character encoding? (Setting
    LC_ALL=en_GB.UTF-8 won't change that since fold appears to ignore the encoding.) What does echo Säge | hd show?

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to David W. Hodgins on Sun Mar 20 21:07:14 2022
    "David W. Hodgins" <dwhodgins@nomail.afraid.org> writes:

    On Sun, 20 Mar 2022 16:30:00 -0400, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
    I don't see how it could be a font issue. The character displays fine,
    and even if it did not, that should have no effect on whether fold
    counts them correctly.

    Looking at the source of fold in coreutils 8.32 I can't see any code
    that could possibly count the UTF-8 encoding of ä as a single character.

    Are you using a single-byte character encoding? (Setting
    LC_ALL=en_GB.UTF-8 won't change that since fold appears to ignore the
    encoding.) What does echo Säge | hd show?

    $ man echo
    [dave@x3 ~]$ echo -n Säge|hexdump -x
    0000000 c353 67a4 0065
    0000005

    Thanks for that. hexdump -c (or -b) makes UTF-8 a bit easier to read, especially when there's an odd number of bytes but I can see what's
    going on.

    c3 a4 is indeed the UTF-8 encoding of ä, so I am at a loss to see how
    the source code for fold that I saw could produce the correct output!
    Maybe I missed something.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Ben Bacarisse on Sun Mar 20 21:38:06 2022
    Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

    "David W. Hodgins" <dwhodgins@nomail.afraid.org> writes:

    On Sun, 20 Mar 2022 16:30:00 -0400, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
    I don't see how it could be a font issue. The character displays fine,
    and even if it did not, that should have no effect on whether fold
    counts them correctly.

    Looking at the source of fold in coreutils 8.32 I can't see any code
    that could possibly count the UTF-8 encoding of ä as a single character. >>>
    Are you using a single-byte character encoding? (Setting
    LC_ALL=en_GB.UTF-8 won't change that since fold appears to ignore the
    encoding.) What does echo Säge | hd show?

    $ man echo
    [dave@x3 ~]$ echo -n Säge|hexdump -x
    0000000 c353 67a4 0065
    0000005

    Thanks for that. hexdump -c (or -b) makes UTF-8 a bit easier to read, especially when there's an odd number of bytes but I can see what's
    going on.

    c3 a4 is indeed the UTF-8 encoding of ä, so I am at a loss to see how
    the source code for fold that I saw could produce the correct output!
    Maybe I missed something.

    I don't think I did miss anything but there is a bug report about this
    that suggests that there is a fedora patch that fixes it. Your distro
    probably has that patch applied. That would be the simplest
    explanation.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to Ben Bacarisse on Sun Mar 20 18:07:41 2022
    On Sun, 20 Mar 2022 17:38:06 -0400, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
    I don't think I did miss anything but there is a bug report about this
    that suggests that there is a fedora patch that fixes it. Your distro probably has that patch applied. That would be the simplest
    explanation.

    Confirmed for Mageia. https://svnweb.mageia.org/packages/updates/8/coreutils/current/SOURCES/coreutils-i18n.patch?revision=1679406&view=markup
    is a patch to support multibyte characters, for the Mageia 8 version of the coreutils
    package, which includes the fold command.

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ottavio Caruso@21:1/5 to Jorgen Grahn on Mon Mar 21 10:17:41 2022
    On 19/03/2022 15:40, Jorgen Grahn wrote:
    On Fri, 2022-03-18, Ottavio Caruso wrote:
    On 18/03/2022 08:28, Jorgen Grahn wrote:
    If your cw-sort function read from stdin instead, could you still fit
    it into your workflow? I'm assuming you're normally calling it from
    some larger script.

    No, I call it from the shell.

    This is supposed to check the frequency of mistakes I make while
    learning Morse code on lcwo.net.

    So, do you then paste the input from some web page? In that case I'd personally prefer:

    % cw-sort
    *paste*
    <EOF>

    to

    % cw-sort *paste*

    followed by manual escaping and quoting.

    But oh, maybe that's what you were asking for: a simple one-size-
    fits-all way you can do that without thinking. I have always assumed
    there is no such thing, so I didn't even read the responses which went
    into quoting.

    If there was such a thing, I would consider using it.

    /Jorgen


    No, I don't paste. I have to manually spot the errors and input them in
    the function.

    Incidentally, I'm [pathetically and not knowing what I'm doing] working
    on an offline version of LCWO, provisionally called "LCWOff". Github
    clone will be ready somewhere in the future.

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)