modify specific columns based on another column by using awk and gsub
Solution 1:
awk can match patterns like grep does, so you almost never need grep and awk in a pipeline.
You could do
awk '
BEGIN {FS = OFS = ","}
$1 ~ /AAA/ && $5 ~ /BGB/ {
if ($2) $2 = $2 / 2
if ($3) $3 = $3 / 2
if ($4) $4 = $4 / 2
if ($6) $6 = $6 / 2
}
1
' file
Or, if you want to make the columns to reduce more dynamic
awk -v "columns=2,3,4,6" '
BEGIN {
FS = OFS = ","
n = split(columns, a, /,/)
for (i=1; i<=n; i++) cols[a[i]]=1
}
$1 ~ /AAA/ && $5 ~ /BGB/ {
for (c in cols) if ($c) $c = $c / 2
}
1
' file