SED / AWK或其他:单行1保持空格字符递增数 [英] sed/awk or other: one-liner to increment a number by 1 keeping spacing characters

查看:139
本文介绍了SED / AWK或其他:单行1保持空格字符递增数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改:我不知道提前其中栏我的数字将是,我想有一个班轮。显然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

和我希望它变成:

eh oh    36

(所以我想保持的间距)

(so I want to keep the spacing)

使用的 AWK 的我没有找到如何做到这一点,到目前为止,我有:

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}'

但是,这给了:

eh oh 36

(其中失去了空格字符,因为分隔符是'')

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

有没有办法问的 AWK 的类似的使用完全相同的字段分隔符为输入了打印输出的的?

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

然后我试过没别的东西,使用awk的的子(..,..)的方法:

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).

然后我尝试使用sed,但毕竟这卡住了:

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

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

我可以做算术从的 SED 的使用到匹配数字的引用,并具有输出不能修改的空格字符数?

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

请注意,这是与我有关Emacs和如何将此应用到一些(大)Emacs的区域问题(使用替换区域使用Emacs的壳命令的区域的),但它不是一个同一个问题:这个人是专门关于如何保持空间与工作时的awk / SED的/ etc

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.

推荐答案

下面是关于 ghostdog74的答案不需要在字符串的结尾被固定数目的变化。 ,而不是依靠数量是在一个特定的位置,这是通过使用匹配来完成的。

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

使用 GSUB 而不是子有将与36代替两个37和22 。如果只有一个就行号,也不要紧,你使用。通过做这种方式,虽然,它可以处理的数字与结尾的空白加上其他非数字字符,可能是有(在一些空白)。

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).

如果您有 GAWK ,您可以用 gensub 这样的字符串中挑选出任意数目(只是设置的值

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

第二(其中= 2 )的数量从22到21和嵌入式空间是preserved。

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

它打破了多条线路上,使其更容易阅读,但它的复制/ pastable。

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

这篇关于SED / AWK或其他:单行1保持空格字符递增数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆