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.
Any hints to achieve this goal?
Regards,
HZ
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.
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.
Any hints to achieve this goal?
Regards,
HZ
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:vim yourfile
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.
/[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.
Or use a script?
Any hints to achieve this goal?
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
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++' :)
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
On Thursday, September 23, 2021 at 10:40:01 AM UTC+8, Tavis Ormandy wrote:
On 2021-09-23, Janis Papanagnou wrote:
Maybe even just awk '$NF++' :)
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
This version doesn't keep the original blanks as they are:
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.
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
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
$ 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
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).
also be considered that can be easily supported by adding
an optional sign in the regexp: /[+-]?[0-9]+$/
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:I could, nay, have solved that with tail recursive macros in vim. It's
(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.
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}' fileThis 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).
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:I could, nay, have solved that with tail recursive macros in vim. It's
(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.
kinda slow, and I only did it that way because it was a file needing
hand editing besides number tweaking.
This script was just asked about in c.l.p.m, so I happen to know itIn Awk substitute /[0-9]+$/ by $NF+1.Thank you. This does the trick:
$ awk '{sub(/[0-9]+$/, $NF+1); print}' file
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
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.)
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
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.
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.
[...] 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". [...]
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 183 |
Nodes: | 16 (1 / 15) |
Uptime: | 15:40:44 |
Calls: | 3,594 |
Calls today: | 3 |
Files: | 11,139 |
Messages: | 3,441,065 |