Sed-增加所有大于x的值

Sed-增加所有大于x的值,sed,Sed,我想增加从某个快照开始的所有快照的快照编号(通过使用sed)。如何从快照=22开始增加快照编号 // 我已经找到了增加整个快照编号的命令 sed-r的/(快照=)([0-9]+)/echo\1$(\2+1))/ge的myInputFile 我有这样一个文件: #----------- snapshot=4 #----------- time=1449929 mem_heap_B=110 mem_heap_extra_B=40 mem_stacks_B=0 heap_tree=peak #----

我想增加从某个快照开始的所有快照的快照编号(通过使用sed)。如何从快照=22开始增加快照编号

// 我已经找到了增加整个快照编号的命令
sed-r的/(快照=)([0-9]+)/echo\1$(\2+1))/ge的myInputFile

我有这样一个文件:

#-----------
snapshot=4
#-----------
time=1449929
mem_heap_B=110
mem_heap_extra_B=40
mem_stacks_B=0
heap_tree=peak
#-----------
snapshot=5
#-----------
time=1449929
mem_heap_B=110
mem_heap_extra_B=40
mem_stacks_B=0
heap_tree=peak
.
.
.
#-----------
snapshot=22
#-----------
time=1448920
mem_heap_B=10
mem_heap_extra_B=24
mem_stacks_B=0
heap_tree=detailed
.
.
.
#-----------
snapshot=46
#-----------
time=1449964
mem_heap_B=110
mem_heap_extra_B=24
mem_stacks_B=0
heap_tree=detailed
.
.
.
#-----------
snapshot=172
#-----------
time=1448920
mem_heap_B=10
我希望获得以下输出:

#-----------
snapshot=4
#-----------
time=1449929
mem_heap_B=110
mem_heap_extra_B=40
mem_stacks_B=0
heap_tree=peak
#-----------
snapshot=5
#-----------
time=1449929
mem_heap_B=110
mem_heap_extra_B=40
mem_stacks_B=0
heap_tree=peak
.
.
.
#-----------
snapshot=23
#-----------
time=1448920
mem_heap_B=10
mem_heap_extra_B=24
mem_stacks_B=0
heap_tree=detailed
.
.
.
#-----------
snapshot=47
#-----------
time=1449964
mem_heap_B=110
mem_heap_extra_B=24
mem_stacks_B=0
heap_tree=detailed
.
.
.
#-----------
snapshot=173
#-----------
time=1448920
mem_heap_B=10

您会发现使用sed很难做到这一点,因为sed没有内置的算术功能(并且用纯sed实现它不是一个明智的想法)

但是,使用awk非常简单:

awk -F = 'BEGIN { OFS = FS } $1 == "snapshot" && $2 >= 22 { ++$2 } 1' filename
其工作原理是在
=
分隔符处将行拆分为字段,然后:

BEGIN {
  OFS = FS                      # use the same separator in the output
}                               # as for the input

$1 == "snapshot" && $2 >= 22 {  # if the first field in a line is "snapshot"
                                # and the second larger than or equal to 22:

  ++$2                          # increment the second field
}
1                               # print the resulting line (unchanged if the
                                # condition was not met)