我想在插入中添加100,在下面尝试过,但它会为它添加1美元?
#!/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}'
打印
17 files changed, 441 insertions(+), 49 deletions(-)
17 files changed, 458 insertions(+), 49 deletions(-)
我希望它可以打印541个插入内容。
还有更好的方法吗?
答案 0 :(得分:2)
使用awk变量(使用GNU awk测试):
awk -v l=$lines_added '{ print $1 " " $2 " " $3 " " $4+l " " $5 " " $6 " " $7}'
甚至更简洁:
$ echo $change_summary | awk -v l=$lines_added '{ $4 += l; print}'
17 files changed, 541 insertions(+), 49 deletions(-)
答案 1 :(得分:0)
你应该"取消引用" $lines_added
:
~$ echo $change_summary | awk '{ print $1 " " $2 " " $3 " " ($4)+'$lines_added' " " $5 " " $6 " " $7}'
`17 files changed, 541 insertions(+), 49 deletions(-)
因为它是一个bash变量,而不是一个awk变量。