Add a number all the the values and log transform them using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add a number all the the values and log transform them using awk
# 1  
Old 10-29-2014
Add a number all the the values and log transform them using awk

Hi, is it possible to add a number to all the values in a file and log transform them using awk. thanx in advance


input

Code:
name    c1      c2      c3      c4
r1      0       0.2     0.3     0.4
r2      0       0       0       1
r3      1       0       0       0
r4      0       0       1       1
r5      32      23      11      11

add 1 to every value

Code:
name    c1      c2      c3      c4
r1      1       1.2     1.3     1.4
r2      1       1       1       2
r3      2       1       1       1
r4      33      24      12      12

log2 transform every value

Code:
name    c1      c2      c3      c4
r1      log2(1) log2(1.2)       log2(1.3)       log2(1.4)
r2      log2(1) log2(1) log2(1) log2(2)
r3      log2(2) log2(1) log2(1) log2(1)
r4      log2(33)        log2(24)        log2(12)        log2(12)

divided every value by the sum of that specific row

Code:
name    c1      c2      c3      c4
r1      log2(1)/log2(1)+log2(1.2)+log2(1.3)+log2(1.4)   log2(1.2)/log2(1)+log2(1.2)+log2(1.3)+log2(1.4) log2(1.3)/log2(1)+log2(1.2)+log2(1.3)+log2(1.4) log2(1.4)/log2(1)+log2(1.2)+log2(1.3)+log2(1.4)
r2      log2(1)/log2(1)+log2(1)+log2(1)+log2(2) log2(1)/log2(1)+log2(1)+log2(1)+log2(2) log2(1)/log2(1)+log2(1)+log2(1)+log2(2) log2(2)/log2(1)+log2(1)+log2(1)+log2(2)
r3      ...
r4      ..

# 2  
Old 10-29-2014
Not the slightest attempt from your side?
Try
Code:
awk     'NR==1          {ln2=log(2); print; next}
                        {sum=0
                         for (i=2; i<=NF; i++)
                                {$i=log($i+1)/ln2
                                 sum+=$i}
                         for (i=2; i<=NF; i++)
                                $i/=sum
                        }
         1
        ' OFS="\t" CONVFMT="%.4g" file
name    c1      c2      c3      c4
r1      0       0.2334  0.3359  0.4307
r2      0       0       0       1
r3      1       0       0       0
r4      0       0       0.5     0.5
r5      0.3003  0.2729  0.2134  0.2134

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract multiple values from file and add two additional fields

In the attached file I am trying to use awk to extract multiple values and create the tab-delimited desired output. In the output R_Index is a the sequential # and Pre_Enrichment is defaulted to .. I can extract from the values to the side of the keywords, but most are above and I can not... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Add values of similar patterns with awk

so my output is this: session_closed=157 session_opened=151 session_closed=18 session_opened=17 there are two patterns here, but with different values. the two patterns are "session_opened" and "session_closed". i expect there will be many more other patterns. what i want to do is... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

awk to log transform on matrix file

Hi Friends, I have an input matrix file like this Col1 Col2 Col3 Col4 R1 1 2 3 4 R2 4 5 6 7 R3 5 6 7 8 I would like to consider only the numeric values without touching the column header and the row header. I looked up on the forum's search, and I found this. But, I donno how to... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. UNIX for Dummies Questions & Answers

count number of distinct values in each column with awk

Hi ! input: A|B|C|D A|F|C|E A|B|I|C A|T|I|B As the title of the thread says, I would need to get: 1|3|2|4 I tried different variants of this command, but I don't manage to obtain what I need: gawk 'BEGIN{FS=OFS="|"}{for(i=1; i<=NF; i++) a++} END {for (b in a) print b}' input ... (2 Replies)
Discussion started by: beca123456
2 Replies

5. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

6. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

7. UNIX for Dummies Questions & Answers

How to log transform a text file?

How can I log transform (log 2) a text file which is a single column of numbers? (4 Replies)
Discussion started by: evelibertine
4 Replies

8. UNIX for Dummies Questions & Answers

Using awk to log transform a column in a tab-delimited text file?

How do I use awk to log transform the fifth column of a tab-delimited text file? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

AWK help to add up sequential values

Hello All! As a beginner user i want to add up sequential values in a text file and want to print total sum as output.The Text file will have values like the following: class1{root}>less SUM.txt 1140.00 1155.00 1183.00 ... # it continues # i tried to write a... (1 Reply)
Discussion started by: EAGL€
1 Replies

10. UNIX for Dummies Questions & Answers

Use awk to log transform

Hello. I'm trying to use the awk command to convert certains fields to lgo base 2, not base 10. I'm trying command lines like: awk '$2 $5 $7 {print log(2)($2), log(2)($5), $7) filein.txt > fileout.txt I'm trying to make a deadline. Thanks for helping a newbie. Here's some fresh karma... (1 Reply)
Discussion started by: jmzcal
1 Replies
Login or Register to Ask a Question