Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The light of a hundred stars does not equal the light of the moon.


devel / comp.unix.shell / Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

SubjectAuthor
* Re: Add 1 to each number in a column and keep the originalJanis Papanagnou
`* Re: Add 1 to each number in a column and keep the originalhongy...@gmail.com
 +* Re: Add 1 to each number in a column and keep the originalJanis Papanagnou
 |+* Re: Add 1 to each number in a column and keep the originalTavis Ormandy
 ||`* Re: Add 1 to each number in a column and keep the originalhongy...@gmail.com
 || `- Re: Add 1 to each number in a column and keep the originalTavis Ormandy
 |`- Re: Add 1 to each number in a column and keep the originalKenny McCormack
 `* Re: Add 1 to each number in a column and keep the original format/blank in the mEli the Bearded
  +- Re: Add 1 to each number in a column and keep the original format/blank in the mEli the Bearded
  +* Re: Add 1 to each number in a column and keep the originalJanis Papanagnou
  |`- Re: Add 1 to each number in a column and keep the originalKenny McCormack
  `* Re: Add 1 to each number in a column and keep the originalhongy...@gmail.com
   `* Re: Add 1 to each number in a column and keep the originalJanis Papanagnou
    +* Re: Add 1 to each number in a column and keep the originalJanis Papanagnou
    |`- Re: Add 1 to each number in a column and keep the originalJanis Papanagnou
    `* Re: Add 1 to each number in a column and keep the originalEli the Bearded
     `* Re: Add 1 to each number in a column and keep the originalJanis Papanagnou
      `- Re: Add 1 to each number in a column and keep the originalJanis Papanagnou

1
Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<sigjgg$lu0$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4403&group=comp.unix.shell#4403

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 02:58:56 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sigjgg$lu0$1@gioia.aioe.org>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="22464"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Thu, 23 Sep 2021 00:58 UTC

On 23.09.21 02:06, hongy...@gmail.com wrote:
> I have some stuff in a file like the following:
>
> you're right 1
> foo 2
> bar 3
> [...]
>
>
> I want to add 1 to each number in the last column and keep the original format/blank in the middle part of the line.

vim yourfile
/[0-9]\+$
^A
n ^A
....
n ^A

(where ^A means to type <Ctrl>-A).

>
> Any hints to achieve this goal?

Or use a script?

In Awk substitute /[0-9]+$/ by $NF+1.

Janis

>
> Regards,
> HZ
>

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4404&group=comp.unix.shell#4404

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a37:747:: with SMTP id 68mr2435664qkh.526.1632359594665;
Wed, 22 Sep 2021 18:13:14 -0700 (PDT)
X-Received: by 2002:a05:620a:288b:: with SMTP id j11mr2294057qkp.175.1632359594361;
Wed, 22 Sep 2021 18:13:14 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Wed, 22 Sep 2021 18:13:14 -0700 (PDT)
In-Reply-To: <sigjgg$lu0$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=54.180.6.193; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 54.180.6.193
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com> <sigjgg$lu0$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Thu, 23 Sep 2021 01:13:14 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 38
 by: hongy...@gmail.com - Thu, 23 Sep 2021 01:13 UTC

On Thursday, September 23, 2021 at 8:58:59 AM UTC+8, Janis Papanagnou wrote:
> On 23.09.21 02:06, hongy...@gmail.com wrote:
> > I have some stuff in a file like the following:
> >
> > you're right 1
> > foo 2
> > bar 3
> > [...]
> >
> >
> > I want to add 1 to each number in the last column and keep the original format/blank in the middle part of the line.
> vim yourfile
> /[0-9]\+$
> ^A
> n
> ^A
> ...
> n
> ^A
>
> (where ^A means to type <Ctrl>-A).

This seems to require a lot of keystrokes. Therefore, for a large file, it may not be appropriate.

> >
> > Any hints to achieve this goal?
> Or use a script?
>
> In Awk substitute /[0-9]+$/ by $NF+1.

Thank you. This does the trick:

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

In Emacs, perform the following on the selected region, as noted here [1]:

C-u M-x shell-command-on-region RET awk '{sub(/[0-9]+$/, $NF+1); print}' RET

[1] https://stackoverflow.com/a/2686806

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<sigl0i$139t$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4405&group=comp.unix.shell#4405

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 03:24:34 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sigl0i$139t$1@gioia.aioe.org>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org>
<15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="36157"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Thu, 23 Sep 2021 01:24 UTC

On 23.09.21 03:13, hongy...@gmail.com wrote:
> On Thursday, September 23, 2021 at 8:58:59 AM UTC+8, Janis Papanagnou wrote:
>> On 23.09.21 02:06, hongy...@gmail.com wrote:
>>> I have some stuff in a file like the following:
>>>
>>> you're right 1
>>> foo 2
>>> bar 3
>>> [...]
>>>
>>>
>>> I want to add 1 to each number in the last column and keep the original format/blank in the middle part of the line.
>> vim yourfile
>> /[0-9]\+$
>> ^A
>> n
>> ^A
>> ...
>> n
>> ^A
>>
>> (where ^A means to type <Ctrl>-A).
>
> This seems to require a lot of keystrokes. Therefore, for a large file, it may not be appropriate.

Of course, but still valid given your under-specified requirements.
For small files an in-place editor solution may be more appropriate.
But to be honest, I didn't expect that you'd have asked for an editor
solution.

>
>>>
>>> Any hints to achieve this goal?
>> Or use a script?
>>
>> In Awk substitute /[0-9]+$/ by $NF+1.
>
> Thank you. This does the trick:
>
> $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

Which can be simplified if you have substitutions on every line to

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

Janis

>
> In Emacs, perform the following on the selected region, as noted here [1]:
>
> C-u M-x shell-command-on-region RET awk '{sub(/[0-9]+$/, $NF+1); print}' RET
>
> [1] https://stackoverflow.com/a/2686806
>

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<ir27ntFg2qrU1@mid.individual.net>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4406&group=comp.unix.shell#4406

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: tav...@gmail.com (Tavis Ormandy)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: 23 Sep 2021 02:39:57 GMT
Lines: 19
Message-ID: <ir27ntFg2qrU1@mid.individual.net>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org>
<15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
<sigl0i$139t$1@gioia.aioe.org>
X-Trace: individual.net O56g7OwO8E1UJt6XuhCSpQoZSeNpZd2z5bUl/1Ybbov3R131Td
Cancel-Lock: sha1:kpHfQ7k+rJ51rOBO+CgPkBTfJQU=
User-Agent: slrn/pre1.0.4-5 (Linux)
 by: Tavis Ormandy - Thu, 23 Sep 2021 02:39 UTC

On 2021-09-23, Janis Papanagnou wrote:
>>
>> Thank you. This does the trick:
>>
>> $ awk '{sub(/[0-9]+$/, $NF+1); print}' file
>
> Which can be simplified if you have substitutions on every line to
>
> $ awk 'sub(/[0-9]+$/,$NF+1)' file
>

Maybe even just awk '$NF++' :)

Tavis.

--
_o) $ lynx lock.cmpxchg8b.com
/\\ _o) _o) $ finger taviso@sdf.org
_\_V _( ) _( ) @taviso

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<91012cc0-924e-4a9e-9eeb-58231f9bd0e3n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4407&group=comp.unix.shell#4407

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a0c:c189:: with SMTP id n9mr2087412qvh.5.1632365709310;
Wed, 22 Sep 2021 19:55:09 -0700 (PDT)
X-Received: by 2002:ac8:7d0c:: with SMTP id g12mr2710448qtb.214.1632365709138;
Wed, 22 Sep 2021 19:55:09 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Wed, 22 Sep 2021 19:55:08 -0700 (PDT)
In-Reply-To: <ir27ntFg2qrU1@mid.individual.net>
Injection-Info: google-groups.googlegroups.com; posting-host=103.142.140.223; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 103.142.140.223
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org> <15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
<sigl0i$139t$1@gioia.aioe.org> <ir27ntFg2qrU1@mid.individual.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <91012cc0-924e-4a9e-9eeb-58231f9bd0e3n@googlegroups.com>
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Thu, 23 Sep 2021 02:55:09 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 26
 by: hongy...@gmail.com - Thu, 23 Sep 2021 02:55 UTC

On Thursday, September 23, 2021 at 10:40:01 AM UTC+8, Tavis Ormandy wrote:
> On 2021-09-23, Janis Papanagnou wrote:
> >>
> >> Thank you. This does the trick:
> >>
> >> $ awk '{sub(/[0-9]+$/, $NF+1); print}' file
> >
> > Which can be simplified if you have substitutions on every line to
> >
> > $ awk 'sub(/[0-9]+$/,$NF+1)' file
> >
> Maybe even just awk '$NF++' :)

This version doesn't keep the original blanks as they are:

(datasci) werner@X10DAi-00:~$ cat 1111
you're right 1
fdf 2

(datasci) werner@X10DAi-00:~$ awk 'sub(/[0-9]+$/,$NF+1)' 1111
you're right 2
fdf 3
(datasci) werner@X10DAi-00:~$ awk '$NF++' 1111
you're right 2
fdf 3

HZ

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<ir2b2tFg6h6U1@mid.individual.net>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4408&group=comp.unix.shell#4408

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: tav...@gmail.com (Tavis Ormandy)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: 23 Sep 2021 03:37:01 GMT
Lines: 25
Message-ID: <ir2b2tFg6h6U1@mid.individual.net>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org>
<15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
<sigl0i$139t$1@gioia.aioe.org> <ir27ntFg2qrU1@mid.individual.net>
<91012cc0-924e-4a9e-9eeb-58231f9bd0e3n@googlegroups.com>
X-Trace: individual.net tmzF+mkPhDXgef5iK8WWfQ2xTc/xW8Cv7A6PmV5wJRWMLGezqD
Cancel-Lock: sha1:N4uWFxYl1zvvY5FilAaK2OZTlok=
User-Agent: slrn/pre1.0.4-5 (Linux)
 by: Tavis Ormandy - Thu, 23 Sep 2021 03:37 UTC

On 2021-09-23, hongy...@gmail.com wrote:
> On Thursday, September 23, 2021 at 10:40:01 AM UTC+8, Tavis Ormandy wrote:
>> On 2021-09-23, Janis Papanagnou wrote:
>> >>
>> >> Thank you. This does the trick:
>> >>
>> >> $ awk '{sub(/[0-9]+$/, $NF+1); print}' file
>> >
>> > Which can be simplified if you have substitutions on every line to
>> >
>> > $ awk 'sub(/[0-9]+$/,$NF+1)' file
>> >
>> Maybe even just awk '$NF++' :)
>
> This version doesn't keep the original blanks as they are:
>

Ahh, true, they'll be OFS separated. Too bad, it was so nice and clean :)

Tavis.

--
_o) $ lynx lock.cmpxchg8b.com
/\\ _o) _o) $ finger taviso@sdf.org
_\_V _( ) _( ) @taviso

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<eli$2109230114@qaz.wtf>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4409&group=comp.unix.shell#4409

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.panix5.panix.com!qz!not-for-mail
From: *...@eli.users.panix.com (Eli the Bearded)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 05:14:36 -0000 (UTC)
Organization: Some absurd concept
Message-ID: <eli$2109230114@qaz.wtf>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com> <sigjgg$lu0$1@gioia.aioe.org> <15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
Injection-Date: Thu, 23 Sep 2021 05:14:36 -0000 (UTC)
Injection-Info: reader1.panix.com; posting-host="panix5.panix.com:166.84.1.5";
logging-data="1526"; mail-complaints-to="abuse@panix.com"
User-Agent: Vectrex rn 2.1 (beta)
X-Liz: It's actually happened, the entire Internet is a massive game of Redcode
X-Motto: "Erosion of rights never seems to reverse itself." -- kenny@panix
X-US-Congress: Moronic Fucks.
X-Attribution: EtB
XFrom: is a real address
Encrypted: double rot-13
 by: Eli the Bearded - Thu, 23 Sep 2021 05:14 UTC

In comp.unix.shell, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:
> On Thursday, September 23, 2021 at 8:58:59 AM UTC+8, Janis Papanagnou wrote:
>> (where ^A means to type <Ctrl>-A).
> This seems to require a lot of keystrokes. Therefore, for a large file, it may
> not be appropriate.

I could, nay, have solved that with tail recursive macros in vim. It's
kinda slow, and I only did it that way because it was a file needing
hand editing besides number tweaking.

>> In Awk substitute /[0-9]+$/ by $NF+1.
> Thank you. This does the trick:
>
> $ awk '{sub(/[0-9]+$/, $NF+1); print}' file

This script was just asked about in c.l.p.m, so I happen to know it
fails.

$ cat file
0 a1
aa 2
aaa 3 4
bbb
ccc -2
-1
abc -2
cba --3
cab ---4
bac ----5
acb -----6
$

All of the lines with letters then negative numbers do not meet the
requirement of add one to them.

$ awk '{sub(/[0-9]+$/, $NF+1); print}' /tmp/file
1 a1
aa 3
aaa 3 5
bbb
ccc --1
-0
abc --1
cba --1
cab ---1
bac ----1
acb -----1
$ awk --version
awk version 20121220
$

I got the same results with gawk (GNU Awk 5.0.1).

> In Emacs, perform the following on the selected region, as noted here [1]:
>
> C-u M-x shell-command-on-region RET awk '{sub(/[0-9]+$/, $NF+1); print}' RET

In vim, I'd set a mark at start and end and (say 'ma' and 'mb') then

:'a,'b ! awk ... RET

There's certainly more than one way to do that, though. But first I'd
want the awk command that works.

My perl solution, which does add one to every natural number, and
preserves white space, was this:

perl -wpe 's/\b[0-9]+$/$&+1/e' file

Elijah
------
adding one to all integers or all reals needs a different solution

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<eli$2109230206@qaz.wtf>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4410&group=comp.unix.shell#4410

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.panix5.panix.com!qz!not-for-mail
From: *...@eli.users.panix.com (Eli the Bearded)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 06:14:56 -0000 (UTC)
Organization: Some absurd concept
Message-ID: <eli$2109230206@qaz.wtf>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com> <sigjgg$lu0$1@gioia.aioe.org> <15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com> <eli$2109230114@qaz.wtf>
Injection-Date: Thu, 23 Sep 2021 06:14:56 -0000 (UTC)
Injection-Info: reader1.panix.com; posting-host="panix5.panix.com:166.84.1.5";
logging-data="18268"; mail-complaints-to="abuse@panix.com"
User-Agent: Vectrex rn 2.1 (beta)
X-Liz: It's actually happened, the entire Internet is a massive game of Redcode
X-Motto: "Erosion of rights never seems to reverse itself." -- kenny@panix
X-US-Congress: Moronic Fucks.
X-Attribution: EtB
XFrom: is a real address
Encrypted: double rot-13
 by: Eli the Bearded - Thu, 23 Sep 2021 06:14 UTC

In comp.unix.shell, I wrote:
> My perl solution, which does add one to every natural number, and
> preserves white space, was this:
>
> perl -wpe 's/\b[0-9]+$/$&+1/e' file

I should note that for most purposes that's wrong. It doesn't consider
negative values when adding one, and it doesn't enforce a white spaces
separation of numbers from all other characters.

This is a tad better, but should ---5 be treated as a number? Probably
not.

perl -wpe 's/\b-?[0-9]+$/$&+1/e' file

Elijah
------
it's too late to come up with a good short accurate version

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<sih6m4$2ep7h$2@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4411&group=comp.unix.shell#4411

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 06:26:12 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sih6m4$2ep7h$2@news.xmission.com>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com> <sigjgg$lu0$1@gioia.aioe.org> <15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com> <sigl0i$139t$1@gioia.aioe.org>
Injection-Date: Thu, 23 Sep 2021 06:26:12 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2581745"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Thu, 23 Sep 2021 06:26 UTC

In article <sigl0i$139t$1@gioia.aioe.org>,
Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
....
>> $ awk '{sub(/[0-9]+$/, $NF+1); print}' file
>
>Which can be simplified if you have substitutions on every line to
>
>$ awk 'sub(/[0-9]+$/,$NF+1)' file

I usually do this sort of thing as:

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

But note that, sometimes, the filtering action (of skipping lines that are
unexpected) is actually desirable - in which case, you don't want the +1.

--
"If our country is going broke, let it be from feeding the poor and caring for
the elderly. And not from pampering the rich and fighting wars for them."

--Living Blue in a Red State--

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<f46957c8-63cb-5f09-6b0e-0b431e5010ba@hotmail.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4413&group=comp.unix.shell#4413

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 11:01:55 +0200
Organization: Aioe.org NNTP Server
Message-ID: <f46957c8-63cb-5f09-6b0e-0b431e5010ba@hotmail.com>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org>
<15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
<eli$2109230114@qaz.wtf>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="580"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Janis Papanagnou - Thu, 23 Sep 2021 09:01 UTC

On 23.09.21 07:14, Eli the Bearded wrote:
> In comp.unix.shell, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:
>> On Thursday, September 23, 2021 at 8:58:59 AM UTC+8, Janis Papanagnou wrote:
>>> (where ^A means to type <Ctrl>-A).
>> This seems to require a lot of keystrokes. Therefore, for a large file, it may
>> not be appropriate.
>
> I could, nay, have solved that with tail recursive macros in vim. It's
> kinda slow, and I only did it that way because it was a file needing
> hand editing besides number tweaking.
>
>>> In Awk substitute /[0-9]+$/ by $NF+1.
>> Thank you. This does the trick:
>>
>> $ awk '{sub(/[0-9]+$/, $NF+1); print}' file
>
> This script was just asked about in c.l.p.m, so I happen to know it
> fails.
>
> $ cat file
> 0
> a1
> aa 2
> aaa 3 4
> bbb
> ccc -2
> -1
> abc -2
> cba --3
> cab ---4
> bac ----5
> acb -----6
> $
>
> All of the lines with letters then negative numbers do not meet the
> requirement of add one to them.
>
> $ awk '{sub(/[0-9]+$/, $NF+1); print}' /tmp/file
> 1
> a1
> aa 3
> aaa 3 5
> bbb
> ccc --1
> -0
> abc --1
> cba --1
> cab ---1
> bac ----1
> acb -----1
> $ awk --version
> awk version 20121220
> $
>
> I got the same results with gawk (GNU Awk 5.0.1).

The OP's examples and description ("columns") indicate that
there's separated numbers, so most of your testcases don't
match the requirement, and for those that do it works. For
the case that negative numbers (or numbers with sign) shall
also be considered that can be easily supported by adding
an optional sign in the regexp: /[+-]?[0-9]+$/

Janis

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<sihlr9$2f1mn$1@news.xmission.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4416&group=comp.unix.shell#4416

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 10:44:57 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sihlr9$2f1mn$1@news.xmission.com>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com> <15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com> <eli$2109230114@qaz.wtf> <f46957c8-63cb-5f09-6b0e-0b431e5010ba@hotmail.com>
Injection-Date: Thu, 23 Sep 2021 10:44:57 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2590423"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Thu, 23 Sep 2021 10:44 UTC

In article <f46957c8-63cb-5f09-6b0e-0b431e5010ba@hotmail.com>,
Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
....
>also be considered that can be easily supported by adding
>an optional sign in the regexp: /[+-]?[0-9]+$/

But then he'll just suggest:

This a plus sign +27

(Where OP wants that to become:

This a plus sign +28
)

You are right, of course, that allowing there to be zero spaces before the
number makes the problem hopelessly muddled.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/FreeCollege

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<6cab65cf-259a-479b-be2d-c523125b4e0en@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4420&group=comp.unix.shell#4420

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a37:a886:: with SMTP id r128mr4820058qke.453.1632404988922;
Thu, 23 Sep 2021 06:49:48 -0700 (PDT)
X-Received: by 2002:a05:622a:5ca:: with SMTP id d10mr4714579qtb.197.1632404988747;
Thu, 23 Sep 2021 06:49:48 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Thu, 23 Sep 2021 06:49:48 -0700 (PDT)
In-Reply-To: <eli$2109230114@qaz.wtf>
Injection-Info: google-groups.googlegroups.com; posting-host=103.142.140.222; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 103.142.140.222
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org> <15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
<eli$2109230114@qaz.wtf>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6cab65cf-259a-479b-be2d-c523125b4e0en@googlegroups.com>
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Thu, 23 Sep 2021 13:49:48 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 55
 by: hongy...@gmail.com - Thu, 23 Sep 2021 13:49 UTC

On Thursday, September 23, 2021 at 1:14:39 PM UTC+8, Eli the Bearded wrote:
> In comp.unix.shell, hongy...@gmail.com <hongy...@gmail.com> wrote:
> > On Thursday, September 23, 2021 at 8:58:59 AM UTC+8, Janis Papanagnou wrote:
> >> (where ^A means to type <Ctrl>-A).
> > This seems to require a lot of keystrokes. Therefore, for a large file, it may
> > not be appropriate.
> I could, nay, have solved that with tail recursive macros in vim. It's
> kinda slow, and I only did it that way because it was a file needing
> hand editing besides number tweaking.
> >> In Awk substitute /[0-9]+$/ by $NF+1.
> > Thank you. This does the trick:
> >
> > $ awk '{sub(/[0-9]+$/, $NF+1); print}' file
> This script was just asked about in c.l.p.m, so I happen to know it
> fails.
>
> $ cat file
> 0
> a1
> aa 2
> aaa 3 4
> bbb
> ccc -2
> -1
> abc -2
> cba --3
> cab ---4
> bac ----5
> acb -----6
> $
>
> All of the lines with letters then negative numbers do not meet the
> requirement of add one to them.
>
> $ awk '{sub(/[0-9]+$/, $NF+1); print}' /tmp/file
> 1
> a1
> aa 3
> aaa 3 5
> bbb
> ccc --1
> -0
> abc --1
> cba --1
> cab ---1
> bac ----1
> acb -----1
> $ awk --version
> awk version 20121220
> $
>
> I got the same results with gawk (GNU Awk 5.0.1).

I got the same results. Why does this happen?

HZ

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<sii2nu$1cao$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4422&group=comp.unix.shell#4422

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 16:24:47 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sii2nu$1cao$1@gioia.aioe.org>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org>
<15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
<eli$2109230114@qaz.wtf>
<6cab65cf-259a-479b-be2d-c523125b4e0en@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="45400"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Thu, 23 Sep 2021 14:24 UTC

On 23.09.21 15:49, hongy...@gmail.com wrote:
> On Thursday, September 23, 2021 at 1:14:39 PM UTC+8, Eli the Bearded wrote:
1>> In comp.unix.shell, hongy...@gmail.com <hongy...@gmail.com> wrote:
>>> On Thursday, September 23, 2021 at 8:58:59 AM UTC+8, Janis Papanagnou wrote:
>>>> (where ^A means to type <Ctrl>-A).
>>> This seems to require a lot of keystrokes. Therefore, for a large file, it may
>>> not be appropriate.
>> I could, nay, have solved that with tail recursive macros in vim. It's
>> kinda slow, and I only did it that way because it was a file needing
>> hand editing besides number tweaking.
>>>> In Awk substitute /[0-9]+$/ by $NF+1.
>>> Thank you. This does the trick:
>>>
>>> $ awk '{sub(/[0-9]+$/, $NF+1); print}' file
>> This script was just asked about in c.l.p.m, so I happen to know it
>> fails.
>>
>> $ cat file
>> 0
>> a1
>> aa 2
>> aaa 3 4
>> bbb
>> ccc -2
>> -1
>> abc -2
>> cba --3
>> cab ---4
>> bac ----5
>> acb -----6
>> $
>>
>> All of the lines with letters then negative numbers do not meet the
>> requirement of add one to them.
>>
>> $ awk '{sub(/[0-9]+$/, $NF+1); print}' /tmp/file
>> 1
>> a1
>> aa 3
>> aaa 3 5
>> bbb
>> ccc --1
>> -0
>> abc --1
>> cba --1
>> cab ---1
>> bac ----1
>> acb -----1
>> $ awk --version
>> awk version 20121220
>> $
>>
>> I got the same results with gawk (GNU Awk 5.0.1).
>
> I got the same results. Why does this happen?

Because $NF addresses the last field, and most samples here have not
a number as last field; $NF does not see a number (a1 is not a number,
--1 is also not a number) like the one seen in your original samples.

If your data would contain elements like
abc def xyz999
(i.e. a different syntax than in your OP) and you'd want as result
abc def xyz1000
then the algorithm needs adjustment to cover that; for example match
the number, get that sub-string and add 1, replace it. (As opposed
to simply access the data using $NF.)

But since your requirements are anyway under-specified, as already
mentioned upthread, it makes no sense to make any assumptions about
potentially irregular data and provide solutions in advance that are
unnecessarily more complex.

Janis

>
> HZ
>

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<sii3na$1so3$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4423&group=comp.unix.shell#4423

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 16:41:46 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sii3na$1so3$1@gioia.aioe.org>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org>
<15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
<eli$2109230114@qaz.wtf>
<6cab65cf-259a-479b-be2d-c523125b4e0en@googlegroups.com>
<sii2nu$1cao$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="62211"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Thu, 23 Sep 2021 14:41 UTC

On 23.09.21 16:24, Janis Papanagnou wrote:
>
> If your data would contain elements like
> abc def xyz999
> (i.e. a different syntax than in your OP) and you'd want as result
> abc def xyz1000
> then the algorithm needs adjustment to cover that; for example match
> the number, get that sub-string and add 1, replace it. (As opposed
> to simply access the data using $NF.)

Here's one way to cover such a requirement (just for illustration, not
sure you really need that with your data)...

awk 'match($0,/[+-]?[0-9]+$/) {
print substr($0,1,RSTART-1) 1+substr($0,RSTART)
}'

Janis

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<siib19$1kap$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4424&group=comp.unix.shell#4424

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 18:46:32 +0200
Organization: Aioe.org NNTP Server
Message-ID: <siib19$1kap$1@gioia.aioe.org>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<sigjgg$lu0$1@gioia.aioe.org>
<15291a40-40e1-4ab7-9a40-dabf5cd5e00cn@googlegroups.com>
<eli$2109230114@qaz.wtf>
<6cab65cf-259a-479b-be2d-c523125b4e0en@googlegroups.com>
<sii2nu$1cao$1@gioia.aioe.org> <sii3na$1so3$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="53593"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Thu, 23 Sep 2021 16:46 UTC

On 23.09.21 16:41, Janis Papanagnou wrote:
> On 23.09.21 16:24, Janis Papanagnou wrote:
>>
>> If your data would contain elements like
>> abc def xyz999
>> (i.e. a different syntax than in your OP) and you'd want as result
>> abc def xyz1000
>> then the algorithm needs adjustment to cover that; for example match
>> the number, get that sub-string and add 1, replace it. (As opposed
>> to simply access the data using $NF.)

I was not perfectly accurate here. With Awk it's of course also
possible to apply that simple $NF access for that case as well;
just define the field separator appropriately, e.g.

awk -F '[^0-9+-]+' 'sub(/[+-]?[0-9]+$/,$NF+1)'

then you can use even the same awk code as previously.

Janis

>
> Here's one way to cover such a requirement (just for illustration, not
> sure you really need that with your data)...
>
> awk 'match($0,/[+-]?[0-9]+$/) {
> print substr($0,1,RSTART-1) 1+substr($0,RSTART)
> }'
>
>
> Janis
>

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<eli$2109231328@qaz.wtf>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4425&group=comp.unix.shell#4425

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.panix5.panix.com!qz!not-for-mail
From: *...@eli.users.panix.com (Eli the Bearded)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 17:28:30 -0000 (UTC)
Organization: Some absurd concept
Message-ID: <eli$2109231328@qaz.wtf>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com> <eli$2109230114@qaz.wtf> <6cab65cf-259a-479b-be2d-c523125b4e0en@googlegroups.com> <sii2nu$1cao$1@gioia.aioe.org>
Injection-Date: Thu, 23 Sep 2021 17:28:30 -0000 (UTC)
Injection-Info: reader1.panix.com; posting-host="panix5.panix.com:166.84.1.5";
logging-data="24004"; mail-complaints-to="abuse@panix.com"
User-Agent: Vectrex rn 2.1 (beta)
X-Liz: It's actually happened, the entire Internet is a massive game of Redcode
X-Motto: "Erosion of rights never seems to reverse itself." -- kenny@panix
X-US-Congress: Moronic Fucks.
X-Attribution: EtB
XFrom: is a real address
Encrypted: double rot-13
 by: Eli the Bearded - Thu, 23 Sep 2021 17:28 UTC

In comp.unix.shell, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
> Because $NF addresses the last field, and most samples here have not
> a number as last field; $NF does not see a number (a1 is not a number,
> --1 is also not a number) like the one seen in your original samples.

I don't like that answer. It does not feel complete. The awk script
will take 'a1' in and spit 'a1' out. But it does NOT take '--2' in
and spit '--2' out.

> But since your requirements are anyway under-specified, as already
> mentioned upthread, it makes no sense to make any assumptions about
> potentially irregular data and provide solutions in advance that are
> unnecessarily more complex.

It was very specified when I started to look at the problem. However
that specification had little to do with the original one.

Subject: The perl version of this command: awk '{sub(/[0-9]+$/, $NF+1); print}'.
Newsgroups: comp.lang.perl.misc
Message-ID: <c903cb65-a01e-4a3b-8443-ae69d470083bn@googlegroups.com>

I attempted the problem, and found that I do not understand what awk is
doing with lines that end /\s-+[0-9]+/ so I couldn't really come up
with a Perl version without hard coding a bunch of cases.

Elijah
------
has comp.unix.shell after comp.lang.perl.misc in reading order

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<siik38$b7$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4426&group=comp.unix.shell#4426

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 21:21:11 +0200
Organization: Aioe.org NNTP Server
Message-ID: <siik38$b7$1@gioia.aioe.org>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<eli$2109230114@qaz.wtf>
<6cab65cf-259a-479b-be2d-c523125b4e0en@googlegroups.com>
<sii2nu$1cao$1@gioia.aioe.org> <eli$2109231328@qaz.wtf>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="359"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Janis Papanagnou - Thu, 23 Sep 2021 19:21 UTC

On 23.09.21 19:28, Eli the Bearded wrote:
> In comp.unix.shell, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
>> Because $NF addresses the last field, and most samples here have not
>> a number as last field; $NF does not see a number (a1 is not a number,
>> --1 is also not a number) like the one seen in your original samples.
>
> I don't like that answer. It does not feel complete. The awk script
> will take 'a1' in and spit 'a1' out. But it does NOT take '--2' in
> and spit '--2' out.

Well, we could also just say "trash in, trash out". Or specify the
task more accurately and in a complete way (presuming the OP has a
clear image of the data and requirements).

Everything that is unspecified (data *and* program!) may behave as
it wants. So what we can try to do is explain why specific random
ad hoc data will lead to that behavior, and I give it another try
for you. The substitution works on a pattern that is not bound to
any fields, it operates on the whole string. Why? Because of the
explicit "keep spacing intact" requirement. The substituted value
on the other hand is taken for simplicity from the last _field_.
That is valid if, as had been assumed, the last field contains a
number (and not sequences of letters or punctuation and digits).
Now you decided to challenge the test samples by such patterns.
That's okay. But because that is outside the presumed specification
we have to examine the behavior of the samples individually. The
string "a2", interpreted as a numeric value by awk, yields the
value 0. Adding 1 results in 1. And replacing the the _numeric_
rest of the line (which is "1") again yields 1; together with the
preceding "a" that's "a1". It would probably have been clearer
if you'd have chosen "a3" as sample value (which would also have
resulted in "a1"). The second sample "--2" is similar. It's not
a valid number, so its interpreted value is 0 and adding 1 is 1.
The substitution operates just on the digit part and replaces the
"2" by the calculated 1.

I think these detailed explanation should now make it obvious how
the results emerged, and I hope you thus "like" this answer more
than the terse one quoted above. But I haven't recapitulated every
single test sample of yours, so if there's still one that puzzles
you please feel free to ask.

Janis

Re: Add 1 to each number in a column and keep the original format/blank in the middle part of the line.

<siikbo$40i$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=4427&group=comp.unix.shell#4427

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Add 1 to each number in a column and keep the original
format/blank in the middle part of the line.
Date: Thu, 23 Sep 2021 21:25:44 +0200
Organization: Aioe.org NNTP Server
Message-ID: <siikbo$40i$1@gioia.aioe.org>
References: <f1037275-d7ff-4694-854d-9a351ed32065n@googlegroups.com>
<eli$2109230114@qaz.wtf>
<6cab65cf-259a-479b-be2d-c523125b4e0en@googlegroups.com>
<sii2nu$1cao$1@gioia.aioe.org> <eli$2109231328@qaz.wtf>
<siik38$b7$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="4114"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Thu, 23 Sep 2021 19:25 UTC

Should have been...

On 23.09.21 21:21, Janis Papanagnou wrote:
> [...] The string "a2",

The string "a1",

> interpreted as a numeric value by awk, yields the
> value 0. Adding 1 results in 1. And replacing the the _numeric_
> rest of the line (which is "1") again yields 1; together with the
> preceding "a" that's "a1". [...]

Sorry for the typo.

Janis

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor