linux - in Shell, How to do arithmetic operation on a number which is part of a string? -


this question has answer here:

i wanted add 100 insertions, tried below, adding $1 instead ?

#!/bin/bash change_summary="17 files changed, 441 insertions(+), 49 deletions(-)" lines_added="100" echo $change_summary echo $change_summary | awk '{ print $1 " " $2 " " $3 " " $4+$lines_added " " $5 " " $6 " " $7}' 

it prints

17 files changed, 441 insertions(+), 49 deletions(-) 17 files changed, 458 insertions(+), 49 deletions(-) 

i expecting print 541 insertions.

is there better way same?

use awk variable (tested gnu awk):

 awk -v l=$lines_added '{ print $1 " " $2 " " $3 " " $4+l " " $5 " " $6 " " $7}' 

or more succinctly:

 $ echo $change_summary | awk -v l=$lines_added '{ $4 += l; print}'  17 files changed, 541 insertions(+), 49 deletions(-) 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -