开发者

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

开发者 https://www.devze.com 2022-12-27 13:26 出处:网络
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?

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

(开发者_如何学Pythonthe 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.


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.


$ echo "eh oh    37" | awk '{n=$NF+1; gsub(/[0-9]+$/,n) }1'
eh oh    38

or

$ echo "eh oh    37" | awk '{n=$NF+1; gsub(/..$/,n) }1'
eh oh    38


something like

number=`echo "eh oh 37" | grep -o '[0-9]*'`
sed 's/$number/`expr $number + 1`/'


How about:

$ echo "eh oh   37" | awk -F'[ \t]' '{$NF = $NF - 1;} 1'
eh oh   36


The solution will not preserve the number of decimals, so if the number is 10, then the result is 9, even if one would like to have 09.

I did not write the shortest possible code, it should stay readable

Here I construct the printf pattern using RLENGTH so it becomes %02d (2 being the length of the matched pattern)

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

精彩评论

暂无评论...
验证码 换一张
取 消