开发者

Perform arithmetic operations on all "cells" in tab-delimited file

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

I want to perform a mathematical operation on values present in the file (say adding 5 to value present in 开发者_如何学JAVAeach column and then dividing it by 12).

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

Thank you in advance.


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'll want to work thru this excellent awk tutorial to really understand what is happening.

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer. Note that you can 'accept' only one answer (with a check mark) and you can vote for up to 30 answers each day.


Example using awk:

gawk '{for (i = 1; i <= NF; i += 1) {printf "%f\t", ($i + 5) / 12;} printf "\n"}'


try sed or awk (awk is very good) they were designed to do that

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜