Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed/awk or other: one-liner to increment a number by 1 keeping spacing characters

Tags:

shell

unix

sed

awk

EDIT: I don't know in advance at which "column" my digits are going to be and I'd like to have a one-liner. Apparently sed doesn't do arithmetic, so maybe a one-liner solution based on awk?

I've got a string: (notice the spacing)

eh oh    37

and I want it to become:

eh oh    36

(so I want to keep the spacing)

Using awk I don't find how to do it, so far I have:

echo "eh oh   37" | awk '$3>=0&&$3<=99 {$3--} {print}'

But this gives:

eh oh 36

(the spacing characters where lost, because the field separator is ' ')

Is there a way to ask awk something like "print the output using the exact same field separators as the input had"?

Then I tried yet something else, using awk's sub(..,..) method:

' sub(/[0-9][0-9]/, ...) {print}'

but no cigar yet: I don't know how to reference the regexp and do arithmetic on it in the second argument (which I left with '...' for now).

Then I tried with sed, but got stuck after this:

echo "eh oh   37" | sed -e 's/\([0-9][0-9]\)/.../' 

Can I do arithmetic from sed using a reference to the matching digits and have the output not modify the number of spacing characters?

Note that it's related to my question concerning Emacs and how to apply this to some (big) Emacs region (using a replace region with Emacs's shell-command-on-region) but it's not an identical question: this one is specifically about how to "keep spaces" when working with awk/sed/etc.

like image 452
SyntaxT3rr0r Avatar asked Dec 22 '22 03:12

SyntaxT3rr0r


1 Answers

Here is a variation on ghostdog74's answer that does not require the number to be anchored at the end of the string. This is accomplished using match instead of relying on the number to be in a particular position.

This will replace the first number with its value minus one:

$ echo "eh oh    37      aaa     22    bb" | awk '{n = substr($0, match($0, /[0-9]+/), RLENGTH) - 1; sub(/[0-9]+/, n); print }'
eh oh    36      aaa     22    bb

Using gsub there instead of sub would replace both the "37" and the "22" with "36". If there's only one number on the line, it doesn't matter which you use. By doing it this way, though, it will handle numbers with trailing whitespace plus other non-numeric characters that may be there (after some whitespace).

If you have gawk, you can use gensub like this to pick out an arbitrary number within the string (just set the value of which):

$ echo "eh oh    37      aaa     22    bb    19" |
    awk -v which=2 'BEGIN { regex = "([0-9]+)\\>[^0-9]*";
        for (i = 1; i < which; i++) {regex = regex"([0-9]+)\\>[^0-9]*"}}
        { match($0, regex, a);
        n = a[which] - 1;                    # do the math
        print gensub(/[0-9]+/, n, which) }'
eh oh    37      aaa     21    bb    19

The second (which=2) number went from 22 to 21. And the embedded spaces are preserved.

It's broken out on multiple lines to make it easier to read, but it's copy/pastable.

like image 94
Dennis Williamson Avatar answered Dec 27 '22 08:12

Dennis Williamson