• The perl version of this command: awk '{sub(/[0-9]+$/, $NF+1); print}'.

    From Hongyi Zhao@21:1/5 to All on Wed Sep 22 18:40:11 2021
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    Regards,
    HZ

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eli the Bearded@21:1/5 to hongyi.zhao@gmail.com on Thu Sep 23 05:00:59 2021
    In comp.lang.perl.misc, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    That finds an all numerical field at the end of a line and adds one
    to it.

    $ cat file
    0
    a1
    aa 2
    aaa 3 4
    bbb
    ccc -2
    $ perl -wpe 's/\b[0-9]+$/$&+1/e' file
    0
    a1
    aa 2
    aaa 3 4
    bbb
    ccc -3
    $
    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file
    1
    a1
    aa 3
    aaa 3 5
    bbb
    ccc --1
    $

    Ugh, yikes. That last line.

    Some more test cases.

    $ cat file
    0
    a1
    aa 2
    aaa 3 4
    bbb
    -0
    01
    -1
    abc -2
    cba --3
    cab ---4
    bac ----5
    acb -----6
    $ awk '{sub(/[0-9]+$/, $NF+1); print}' < /tmp/file
    1
    a1
    aa 3
    aaa 3 5
    bbb
    -1
    2
    -0
    abc --1
    cba --1
    cab ---1
    bac ----1
    acb -----1
    $

    Okay, I give up. I can't explain the abc verus cba lines without a bunch
    of hard coded rules. Someone else who understands what awk is doing may
    have better luck.

    Eiijah
    ------
    never used awk much before starting perl

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Fenner@21:1/5 to All on Thu Sep 23 07:22:56 2021
    Am 23.09.2021 um 03:40 schrieb Hongyi Zhao:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    Regards,
    HZ



    a2p meint dazu:

    af@pi3:~ $ echo '{sub(/[0-9]+$/, $NF+1); print}' | a2p
    #!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
    # this emulates #! processing on NIH machines.
    # (remove #! line above if indigestible)

    eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
    # process any FOO=bar switches

    $, = ' '; # set output field separator
    $\ = "\n"; # set output record separator

    while (<>) {
    chomp; # strip record separator
    @Fld = split(' ', $_, -1);
    ($s_ = '"'.($Fld[($#Fld+1)] + 1).'"') =~ s/&/\$&/g, s/[0-9]+$/eval
    $s_/e;
    print $_;
    }
    af@pi3:~ $



    Andreas

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hongyi Zhao@21:1/5 to Andreas Fenner on Thu Sep 23 07:13:47 2021
    On Thursday, September 23, 2021 at 1:23:02 PM UTC+8, Andreas Fenner wrote:
    Am 23.09.2021 um 03:40 schrieb Hongyi Zhao:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    Regards,
    HZ

    a2p meint dazu:

    af@pi3:~ $ echo '{sub(/[0-9]+$/, $NF+1); print}' | a2p
    #!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
    # this emulates #! processing on NIH machines.
    # (remove #! line above if indigestible)

    eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
    # process any FOO=bar switches

    $, = ' '; # set output field separator
    $\ = "\n"; # set output record separator

    while (<>) {
    chomp; # strip record separator
    @Fld = split(' ', $_, -1);
    ($s_ = '"'.($Fld[($#Fld+1)] + 1).'"') =~ s/&/\$&/g, s/[0-9]+$/eval
    $s_/e;
    print $_;
    }
    af@pi3:~ $

    Thank you so much for letting me know about this tool

    HZ

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eli the Bearded@21:1/5 to Andis_Spamtonne@web.de on Thu Sep 23 18:57:01 2021
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de> wrote:
    a2p meint dazu:

    af@pi3:~ $ echo '{sub(/[0-9]+$/, $NF+1); print}' | a2p
    #!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
    # this emulates #! processing on NIH machines.
    # (remove #! line above if indigestible)

    eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
    # process any FOO=bar switches

    $, = ' '; # set output field separator
    $\ = "\n"; # set output record separator

    while (<>) {
    chomp; # strip record separator
    @Fld = split(' ', $_, -1);
    ($s_ = '"'.($Fld[($#Fld+1)] + 1).'"') =~ s/&/\$&/g, s/[0-9]+$/eval $s_/e;
    print $_;
    }
    af@pi3:~ $

    Nice, but not 100% compatible.

    Your script is in /tmp/pa (for perled-awk), my test file from yesterday
    is in /tmp/file; here's a comparison of output:

    $ perl /tmp/pa < /tmp/file > /tmp/file.pa
    $ awk '{sub(/[0-9]+$/, $NF+1); print}' < /tmp/file > /tmp/file.awk
    $ diff /tmp/file.pa /tmp/file.awk
    3,4c3,4
    < aa 1
    < aaa 3 1
    ---
    aa 3
    aaa 3 5
    6,8c6,8
    < ccc -1
    < -1
    < abc -1
    ---
    ccc --1
    -0
    abc --1
    $

    AWK does something odd with /-+[0-9]+/ "numbers".

    Elijah
    ------
    does not understand what AWK is doing

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eric Pozharski@21:1/5 to Eli the Bearded on Fri Sep 24 11:07:05 2021
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de> wrote:

    a2p meint dazu:

    Probably not that compatible.

    *SKIP*
    AWK does something odd with /-+[0-9]+/ "numbers".

    (~/foo.4AI0R3.test is your last test-file) This suggests, that "$NF"
    isn't actually "number of fields in the current record". It actually
    some weird pointer to the last field in the record and then The Number
    of Fields can be somehow derived from it. And it evaluates accordingly:

    % awk '{print $NF, $NF + 1 }' ~/foo.4AI0R3.test
    0 1
    a1 1
    2 3
    4 5
    bbb 1
    -0 1
    01 2
    -1 0
    -2 -1
    --3 1
    ---4 1
    ----5 1
    -----6 1

    I'm glad I have perl to get number of fields.

    *CUT*

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HASM@21:1/5 to Eric Pozharski on Fri Sep 24 11:19:21 2021
    Eric Pozharski <whynot@pozharski.name> writes:

    This suggests, that "$NF" isn't actually "number of fields in the
    current record".

    The man page says NF, not $NF:
    The variable NF is set to the total number of fields in the input record.

    It actually some weird pointer to the last field in the record

    Yes and no, that is, the weird part :-)

    % awk '{print $NF, $NF + 1 }' ~/foo.4AI0R3.test

    Try this:

    awk '{print NF, $NF, $NF + 1 }' ~/foo.4AI0R3.test

    and then it may become clear what awk is doing.

    -- HASM

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to Eric Pozharski on Fri Sep 24 12:17:56 2021
    Eric Pozharski <whynot@pozharski.name> writes:

    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de> wrote:

    a2p meint dazu:

    Probably not that compatible.

    *SKIP*
    AWK does something odd with /-+[0-9]+/ "numbers".

    (~/foo.4AI0R3.test is your last test-file) This suggests, that "$NF"
    isn't actually "number of fields in the current record". It actually
    some weird pointer to the last field in the record and then The Number
    of Fields can be somehow derived from it. And it evaluates accordingly:

    % awk '{print $NF, $NF + 1 }' ~/foo.4AI0R3.test
    0 1
    a1 1
    2 3
    4 5
    bbb 1
    -0 1
    01 2
    -1 0
    -2 -1
    --3 1
    ---4 1
    ----5 1
    -----6 1

    I'm glad I have perl to get number of fields.

    *CUT*

    NF is an Awk variable that represents the number of fields.

    $0 is the current line. $1, $2, $3, ... are the first, second, third,
    ... fields.

    If there are 3 fields, the NF is 1, and $NF is equivalent to $3 -- i.e.,
    $NF is the last field.

    Awk variable names are identifiers; there are no Perl-like sigils. The
    '$' prefix is used only with the numbered fields of the current line.
    The way that $NF expands to $3 and then to the text of the third field
    is a little surprising if you're accustomed to Perl and/or Bourne-like
    shells.

    One way to think of it is that $ is an operator that takes an integer expression operand and yields a reference to a field (or to the entire
    line if the operand is zero).

    $ echo 100 200 300 | awk '{print NF}'
    3
    $ echo 100 200 300 | awk '{print $NF}'
    300
    $ echo 100 200 300 | awk '{print $NF-1}'
    299
    $ echo 100 200 300 | awk '{print $(NF-1)}'
    200
    $ echo 100 200 300 | awk '{print $(NF-2)}'
    100
    $ echo 100 200 300 | awk '{print $(NF-3)}'
    100 200 300

    And you can have white space between the $ and its operand.

    The GNU Awk manual doesn't *quite* say that $ is an operator, but it
    does discuss how you can use it with a non-constant field number.

    https://www.gnu.org/software/gawk/manual/html_node/Nonconstant-Fields.html

    --
    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 Eric Pozharski@21:1/5 to Keith Thompson on Sat Sep 25 14:06:56 2021
    with <87mto1lr4r.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de>
    wrote:

    *SKIP*
    Awk variable names are identifiers; there are no Perl-like sigils.
    The '$' prefix is used only with the numbered fields of the current
    line. The way that $NF expands to $3 and then to the text of the
    third field is a little surprising if you're accustomed to Perl and/or Bourne-like shells.

    That's my understanding of this syntax now too. Just checked,
    unfortunately...

    *SKIP*
    The GNU Awk manual doesn't *quite* say that $ is an operator, but it
    does discuss how you can use it with a non-constant field number. https://www.gnu.org/software/gawk/manual/html_node/Nonconstant-Fields.html

    This sacred knowledge is of no use to me ('awk' of 'busybox' even fails
    to evaluate NR (looks like it always 1)). Pity (I observe some activity
    up there, but I'm glad my expectations are already downgraded).

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to Eric Pozharski on Sat Sep 25 12:20:07 2021
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87mto1lr4r.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de>
    wrote:

    *SKIP*
    Awk variable names are identifiers; there are no Perl-like sigils.
    The '$' prefix is used only with the numbered fields of the current
    line. The way that $NF expands to $3 and then to the text of the
    third field is a little surprising if you're accustomed to Perl and/or
    Bourne-like shells.

    That's my understanding of this syntax now too. Just checked, unfortunately...

    *SKIP*
    The GNU Awk manual doesn't *quite* say that $ is an operator, but it
    does discuss how you can use it with a non-constant field number.
    https://www.gnu.org/software/gawk/manual/html_node/Nonconstant-Fields.html

    This sacred knowledge is of no use to me ('awk' of 'busybox' even fails
    to evaluate NR (looks like it always 1)). Pity (I observe some activity
    up there, but I'm glad my expectations are already downgraded).

    Really? I have busybox 1.30.1 on Ubuntu, and it works for me.

    $ printf 'line one\nline two\nline three\n' | busybox awk '{print NR, $0}'
    1 line one
    2 line two
    3 line three
    $ echo one two three | busybox awk '{print NF, $NF, $(NF-1)}'
    3 three two
    $

    --
    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 Eric Pozharski@21:1/5 to Keith Thompson on Sun Sep 26 14:19:14 2021
    with <87ee9claxk.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87mto1lr4r.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de>
    wrote:

    *SKIP*
    This sacred knowledge is of no use to me ('awk' of 'busybox' even
    fails to evaluate NR (looks like it always 1)). Pity (I observe some
    activity up there, but I'm glad my expectations are already
    downgraded).
    Really? I have busybox 1.30.1 on Ubuntu, and it works for me.

    Well, 1.31.1 on android doesn't (forced upgrade right now) (also, there
    are multiple builds of busybox). But I understand, there might be
    differences what build-time decisiions have been made (also, dying off
    is an issue too). When time will come somehow will make it through with
    what I've got.

    *CUT*

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to Eric Pozharski on Sun Sep 26 16:31:31 2021
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87ee9claxk.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87mto1lr4r.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de>
    wrote:

    *SKIP*
    This sacred knowledge is of no use to me ('awk' of 'busybox' even
    fails to evaluate NR (looks like it always 1)). Pity (I observe some
    activity up there, but I'm glad my expectations are already
    downgraded).
    Really? I have busybox 1.30.1 on Ubuntu, and it works for me.

    Well, 1.31.1 on android doesn't (forced upgrade right now) (also, there
    are multiple builds of busybox). But I understand, there might be differences what build-time decisiions have been made (also, dying off
    is an issue too). When time will come somehow will make it through with
    what I've got.

    *CUT*

    That's surprising. I don't see anything in the busybox awk.c that
    tells me it's even possible to disable support for NR. Can you
    confirm that the output of this:

    ( echo line 1 ; echo line 2 ) | awk '{print NR}'

    (or use "busybox awk" if necessary) *isn't*

    1
    2

    ? If so, let's continue this on comp.lang.awk. (I haven't cross-posted
    or redirected followups yet; I don't want to start a new discussion in a different newsgroup unless there's something to discuss.)

    --
    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 Eric Pozharski@21:1/5 to Keith Thompson on Mon Sep 27 09:17:13 2021
    with <87k0j2kj70.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87ee9claxk.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87mto1lr4r.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de> >>>>>>> wrote:

    *SKIP*
    ? If so, let's continue this on comp.lang.awk.

    I'd rather not. This deviated far enough from topic (whatever that was; honestly, with OP it's hard to tell). I hope that I've got safeguards installed to avoid traps like this again. I can't imagine that outside
    will be appriciated.

    *CUT*

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to Eric Pozharski on Mon Sep 27 11:53:00 2021
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87k0j2kj70.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87ee9claxk.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87mto1lr4r.fsf@nosuchdomain.example.com> Keith Thompson wrote: >>>>>> Eric Pozharski <whynot@pozharski.name> writes:
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de> >>>>>>>> wrote:

    *SKIP*
    ? If so, let's continue this on comp.lang.awk.

    I'd rather not. This deviated far enough from topic (whatever that was; honestly, with OP it's hard to tell). I hope that I've got safeguards installed to avoid traps like this again. I can't imagine that outside
    will be appriciated.

    *CUT*

    OK, one last response and then I'll bail out of this discussion.

    Upthread, you said that "'awk' of 'busybox' even fails to evaluate NR
    (looks like it always 1)".

    Since NR is a fundamental feature of awk, going back to the original implementation, my tentative conclusion is that this was most likely
    operator error. An implementation of awk not implementing NR correctly
    would be astonishing, requiring strong evidence, which I haven't seen.
    User error seems much more likely.

    Back to Perl.

    --
    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 Certes@21:1/5 to Keith Thompson on Tue Sep 28 00:54:39 2021
    On 27/09/2021 19:53, Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87k0j2kj70.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87ee9claxk.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87mto1lr4r.fsf@nosuchdomain.example.com> Keith Thompson wrote: >>>>>>> Eric Pozharski <whynot@pozharski.name> writes:
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de> >>>>>>>>> wrote:

    *SKIP*
    ? If so, let's continue this on comp.lang.awk.

    I'd rather not. This deviated far enough from topic (whatever that was;
    honestly, with OP it's hard to tell). I hope that I've got safeguards
    installed to avoid traps like this again. I can't imagine that outside
    will be appriciated.

    *CUT*

    OK, one last response and then I'll bail out of this discussion.

    Upthread, you said that "'awk' of 'busybox' even fails to evaluate NR
    (looks like it always 1)".

    Since NR is a fundamental feature of awk, going back to the original implementation, my tentative conclusion is that this was most likely
    operator error. An implementation of awk not implementing NR correctly
    would be astonishing, requiring strong evidence, which I haven't seen.
    User error seems much more likely.

    Back to Perl.

    Is this problem as simple as someone confusing NF with NR?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eric Pozharski@21:1/5 to Certes on Tue Sep 28 09:16:49 2021
    with <sitljv$ot4$1@dont-email.me> Certes wrote:
    On 27/09/2021 19:53, Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87k0j2kj70.fsf@nosuchdomain.example.com> Keith Thompson wrote:
    Eric Pozharski <whynot@pozharski.name> writes:
    with <87ee9claxk.fsf@nosuchdomain.example.com> Keith Thompson wrote: >>>>>> Eric Pozharski <whynot@pozharski.name> writes:
    with <87mto1lr4r.fsf@nosuchdomain.example.com> Keith Thompson wrote: >>>>>>>> Eric Pozharski <whynot@pozharski.name> writes:
    with <eli$2109231453@qaz.wtf> Eli the Bearded wrote:
    In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de> >>>>>>>>>> wrote:

    *SKIP*
    ? If so, let's continue this on comp.lang.awk.
    I'd rather not. This deviated far enough from topic (whatever that
    was; honestly, with OP it's hard to tell). I hope that I've got
    safeguards installed to avoid traps like this again. I can't
    imagine that outside will be appriciated.
    *SKIP*
    Back to Perl.
    Is this problem as simple as someone confusing NF with NR?

    Kind sir, rest assured that your magical seeing is enormous -- indeed,
    it is. I've just checked, 1.31.1 (of android) is fine; 1.20 (of linux)
    is fine too. Also learned valuable lesson (not only records must be multi-field, also tests must be multi-record).

    p.s. See what happens when threre are no safeguards?

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Hongyi Zhao on Tue Sep 28 20:05:32 2021
    Hongyi Zhao <hongyi.zhao@gmail.com> writes:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    perl -ape 's/[0-9]+$/$F[-1]+1/e'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Rainer Weikusat on Tue Sep 28 21:31:32 2021
    Rainer Weikusat <rweikusat@talktalk.net> writes:
    Hongyi Zhao <hongyi.zhao@gmail.com> writes:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    perl -ape 's/[0-9]+$/$F[-1]+1/e'

    Perl version :-):

    perl -ape 's/\d+$/$F[-1]+1/e'

    Without using autosplit:

    perl -pe 's/(-\K)?(\d+)$/"$1$2"+1/e'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Rainer Weikusat on Tue Sep 28 21:38:28 2021
    Rainer Weikusat <rweikusat@talktalk.net> writes:
    Rainer Weikusat <rweikusat@talktalk.net> writes:
    Hongyi Zhao <hongyi.zhao@gmail.com> writes:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    [...]

    Without using autosplit:

    perl -pe 's/(-\K)?(\d+)$/"$1$2"+1/e'

    Doesn't work for non-numerical strings ending with a sequence of
    numbers.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Hongyi Zhao on Fri Oct 1 15:11:49 2021
    Hongyi Zhao <hongyi.zhao@gmail.com> writes:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    Regards,
    HZ

    It's actually possible to do this without autosplit:

    [rw@doppelsaurus]/tmp#cat f
    0
    a1
    aa 2
    aaa 3 4
    bbb
    ccc -2
    dddd abc4
    [rw@doppelsaurus]/tmp#awk '{sub(/[0-9]+$/, $NF+1); print}' f
    1
    a1
    aa 3
    aaa 3 5
    bbb
    ccc --1
    dddd abc1
    [rw@doppelsaurus]/tmp#perl -pe 's/(\S*\K)(\d+)$/1+"$1$2"/e' f
    1
    a1
    aa 3
    aaa 3 5
    bbb
    ccc --1
    dddd abc1

    :-)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Otto J. Makela@21:1/5 to Rainer Weikusat on Fri Oct 1 17:49:39 2021
    Rainer Weikusat <rweikusat@talktalk.net> wrote:

    Hongyi Zhao <hongyi.zhao@gmail.com> writes:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    It's actually possible to do this without autosplit:

    [rw@doppelsaurus]/tmp#cat f
    0
    a1
    aa 2
    aaa 3 4
    bbb
    ccc -2
    dddd abc4
    [rw@doppelsaurus]/tmp#awk '{sub(/[0-9]+$/, $NF+1); print}' f
    1
    a1
    aa 3
    aaa 3 5
    bbb
    ccc --1
    dddd abc1
    [rw@doppelsaurus]/tmp#perl -pe 's/(\S*\K)(\d+)$/1+"$1$2"/e' f
    1
    a1
    aa 3
    aaa 3 5
    bbb
    ccc --1
    dddd abc1

    The awk is of course slightly pathological (substituting the trailing
    numeric string for whatever $NF+1 happens to evaluate to, which quite
    often is 0+1), so this does work a bit nicer with the more unusual
    cases, also understands signs:

    % perl -pe 's/[+-]?\d+$/1+$&/e;' f
    1
    a2
    aa 3
    aaa 3 5
    bbb
    ccc -1
    dddd abc5

    --
    /* * * Otto J. Makela <om@iki.fi> * * * * * * * * * */
    /* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
    /* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
    /* * * Computers Rule 01001111 01001011 * * * * * * */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Otto J. Makela on Fri Oct 1 16:23:15 2021
    om@iki.fi (Otto J. Makela) writes:
    Rainer Weikusat <rweikusat@talktalk.net> wrote:

    Hongyi Zhao <hongyi.zhao@gmail.com> writes:
    What's the perl command corresponding to the following awk code:

    $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

    It's actually possible to do this without autosplit:

    [...]

    [rw@doppelsaurus]/tmp#perl -pe 's/(\S*\K)(\d+)$/1+"$1$2"/e' f
    1
    a1
    aa 3
    aaa 3 5
    bbb
    ccc --1
    dddd abc1

    The awk is of course slightly pathological (substituting the trailing
    numeric string for whatever $NF+1 happens to evaluate to, which quite
    often is 0+1), so this does work a bit nicer with the more unusual
    cases, also understands signs:

    % perl -pe 's/[+-]?\d+$/1+$&/e;' f

    This doesn't work at all because it's not equivalent to the original awk script. It should have --1 in the second-to-last-line and abc1 in the
    last.

    1
    a2
    aa 3
    aaa 3 5
    bbb
    ccc -1
    dddd abc5

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Otto J. Makela@21:1/5 to Rainer Weikusat on Mon Oct 4 19:49:32 2021
    Rainer Weikusat <rweikusat@talktalk.net> wrote:

    om@iki.fi (Otto J. Makela) writes:
    The awk is of course slightly pathological (substituting the trailing
    numeric string for whatever $NF+1 happens to evaluate to, which quite
    often is 0+1), so this does work a bit nicer with the more unusual
    cases, also understands signs:

    % perl -pe 's/[+-]?\d+$/1+$&/e;' f

    This doesn't work at all because it's not equivalent to the original
    awk script. It should have --1 in the second-to-last-line and abc1 in
    the last.

    When I said the awk script is slightly pathological, I meant it due to
    the inconsistent internal logic most likely doesn't really produce the
    results the original coder was expecting, and thus I presented something
    that is simple and perhaps a bit more logical in what it outputs.

    I am sorry to have caused such confusion.
    --
    /* * * Otto J. Makela <om@iki.fi> * * * * * * * * * */
    /* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
    /* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
    /* * * Computers Rule 01001111 01001011 * * * * * * */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Otto J. Makela on Mon Oct 4 20:00:16 2021
    om@iki.fi (Otto J. Makela) writes:
    Rainer Weikusat <rweikusat@talktalk.net> wrote:

    om@iki.fi (Otto J. Makela) writes:
    The awk is of course slightly pathological (substituting the trailing
    numeric string for whatever $NF+1 happens to evaluate to, which quite
    often is 0+1), so this does work a bit nicer with the more unusual
    cases, also understands signs:

    % perl -pe 's/[+-]?\d+$/1+$&/e;' f

    This doesn't work at all because it's not equivalent to the original
    awk script. It should have --1 in the second-to-last-line and abc1 in
    the last.

    When I said the awk script is slightly pathological, I meant it due to
    the inconsistent internal logic most likely doesn't really produce the results the original coder was expecting,

    I don't think the original coder was expecting anything except trying to
    do a "my awk is longer than your perl"-demonstration.

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