how to perform mathematical operations on numbers in a file using perl or awk?
There are quite literally dozens of tools you can use for different manipulation of text files. For the specific case you mention, I would probably use perl:
$ perl -pe 's/\b(\d+)\b/$1 + 10/ge' fileA.txt
RS0255_RS0083:125,134,139,151,153,178,180,190
RS0343_RS0083:122,123,173,185,191
RS0343_RS0255:104,111,117,174,189,193
gawk '$0+0 == $0 {$0 += 10} {ORS = RT} 1' RS='[:,\n]' file
An awk
answer: Parse each line as two :
-delimited fields. Use split()
on the second field to split it into fields on the commas, and modify the split-up fields in a loop, creating a new output record. Then output the new record with commas as delimiters, and with the original first field as a "prefix".
awk -F : '
BEGIN { OFS = "," }
{
prefix = $1
nf = split($2,a,",")
$0 = ""
for (i = 1; i <= nf; ++i)
$i = a[i] + 10
printf "%s:%s\n", prefix, $0
}' fileA.txt
The output would be
RS0255_RS0083:125,134,139,151,153,178,180,190
RS0343_RS0083:122,123,173,185,191
RS0343_RS0255:104,111,117,174,189,193
More compact presentation of the above code (for those of you that thinks one-lines are somehow "better"):
awk -F: '{p=$1;n=split($2,a,",");$0="";for(i=1;i<n;++i)$i=a[i]+10;printf "%s:%s\n",p,$0}' OFS=, fileA.txt
Older answer, which is not as "nice" as the above one:
Using awk
and interpreting each line as a list of fields delimited by :
or ,
, adding 10 to the 2nd field onward on each line:
awk -F '[:,]' 'BEGIN { OFS="," } { for (i = 2; i <= NF; ++i) $i += 10 }; 1' fileA.txt
This would give you
RS0255_RS0083,125,134,139,151,153,178,180,190
RS0343_RS0083,122,123,173,185,191
RS0343_RS0255,104,111,117,174,189,193
To change the first comma on each line back to a :
, use sed 's/,/:/'
:
awk -F '[:,]' 'BEGIN { OFS="," } { for (i = 2; i <= NF; ++i) $i += 10 }; 1' fileA.txt |
sed 's/,/:/'