Perform arithmetic operations in all "cells" in the tab delimited file

I have a tab delimited file n by m (where n is the number of rows and m is the number of columns).

I want to perform a mathematical operation on the values ​​present in a file (for example, adding 5 to the value present in each column and then dividing it by 12).

any regex line or a mixture of things .... help

Thanks in advance.

+3
source share
3 answers

An example of using awk:

gawk '{for (i = 1; i <= NF; i += 1) {printf "%f\t", ($i + 5) / 12;} printf "\n"}'
+1
source
awk '{
   # add all numbers on a line
   tot=0
   for (i=1;i<=NF;i++) tot+=$i

   # print detail
   print "LineNo=" NR "\ttot="tot "\tavg=" tot/12 "data=" $0 
   gTot+=tot
   }
   END {
         print "Number of Lines =" NR "\n" \
                GrandTotal=\t" gTot
       }
   ' yourFile

You want to work on this great awk tutorial to really understand what is going on.

Hope this helps.

P.S. , , , , / + ( -) . , "" ( ), 30 .

+2

try sed or awk (awk is very good), they were designed for this

+1
source

All Articles