找到特殊字符后的数字并将其乘以 awk

找到特殊字符后的数字并将其乘以 awk

我有一个文件:

"UDP", "x", 12880, "any", "any", x, 0.0.0.0/0, 0.0.0.0/0, {85000, drop, -1, fast+dsample:1000}, {1m, alert, -1, none}

我怎样才能更改“{”后面的 85000 数字并将其乘以一个值以通过 awk 减少它。例如:85000*0.75= 63,750 最后一行应该是:

"UDP", "x", 12880, "any", "any", x, 0.0.0.0/0, 0.0.0.0/0, {63750, drop, -1, fast+dsample:1000}, {1m, alert, -1, none}

答案1

使用任何 awk(例如 mawk):

mawk 'match($0,/{[0-9]+/) {
  d = substr($0,RSTART+1,RLENGTH)
  $0 = substr($0,1,RSTART) 0.75*d substr($0,RSTART+RLENGTH)
} 1' file
"UDP", "x", 12880, "any", "any", x, 0.0.0.0/0, 0.0.0.0/0, {63750, drop, -1, fast+dsample:1000}, {1m, alert, -1, none}

使用 GNU awk:

gawk 'match($0,/([^{]*{)([0-9]+)(.*)/,a){$0 = a[1] 0.75*a[2] a[3]} 1' file
"UDP", "x", 12880, "any", "any", x, 0.0.0.0/0, 0.0.0.0/0, {63750, drop, -1, fast+dsample:1000}, {1m, alert, -1, none}

或者,使用 perl

perl -pe 's/(?<={)\d+/0.75 * $&/e' file
"UDP", "x", 12880, "any", "any", x, 0.0.0.0/0, 0.0.0.0/0, {63750, drop, -1, fast+dsample:1000}, {1m, alert, -1, none}

相关内容