What's the perl command corresponding to the following awk code:
$ awk '{sub(/[0-9]+$/, $NF+1); print}' file
What's the perl command corresponding to the following awk code:
$ awk '{sub(/[0-9]+$/, $NF+1); print}' file
Regards,
HZ
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:~ $
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:~ $
aa 36,8c6,8
aaa 3 5
ccc --1$
-0
abc --1
In comp.lang.perl.misc, Andreas Fenner <Andis_Spamtonne@web.de> wrote:
a2p meint dazu:
AWK does something odd with /-+[0-9]+/ "numbers".
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
% awk '{print $NF, $NF + 1 }' ~/foo.4AI0R3.test
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*
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:
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.
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
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).
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:
This sacred knowledge is of no use to me ('awk' of 'busybox' evenReally? I have busybox 1.30.1 on Ubuntu, and it works for me.
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).
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' evenReally? I have busybox 1.30.1 on Ubuntu, and it works for me.
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).
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*
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:
? If so, let's continue this on comp.lang.awk.
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*
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.
On 27/09/2021 19:53, Keith Thompson wrote:*SKIP*
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.
Back to Perl.Is this problem as simple as someone confusing NF with NR?
What's the perl command corresponding to the following awk code:
$ awk '{sub(/[0-9]+$/, $NF+1); print}' file
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'
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'
What's the perl command corresponding to the following awk code:
$ awk '{sub(/[0-9]+$/, $NF+1); print}' file
Regards,
HZ
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
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
1
a2
aa 3
aaa 3 5
bbb
ccc -1
dddd abc5
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.
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,
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (3 / 13) |
Uptime: | 70:02:39 |
Calls: | 6,656 |
Calls today: | 2 |
Files: | 12,200 |
Messages: | 5,332,146 |
Posted today: | 1 |