Reputation: 25
I want to increment snapshots numbering of all snapshots starting from certain snapshot (by using sed). How to increment snapshot numbering starting for example from snapshot=22?
//
I have already found the command which increment whole snapshot numbering
sed -r 's/(snapshot=)([0-9]+)/echo \1$((\2+1))/ge' myInputFile
I have file which looks like that:
#-----------
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
I want to get following output:
#-----------
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
Upvotes: 0
Views: 40
Reputation: 44063
You will find it difficult to do that with sed because sed doesn't have built-in arithmetic functionality (and implementing it in pure sed is not a sane idea).
However, it is quite simple with awk:
awk -F = 'BEGIN { OFS = FS } $1 == "snapshot" && $2 >= 22 { ++$2 } 1' filename
This works by splitting lines into fields at =
separators, and then:
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)
Upvotes: 2