How to loop my awk script across different columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to loop my awk script across different columns?
# 1  
Old 04-11-2014
How to loop my awk script across different columns?

input

Code:
name    a1    a2    a3
g1    0.8    0.4    0.2
g2    -0.2    -0.6    -0.7
g3    0.1    0.6    0.8
g4    0.1    0    0
g5    -0.2    -0.2    -0.2
g6    -0.1    -0.9    -0.9
g7    0    0    0.2

output
Code:
a1:2
a2:0
a3:0

I use the following

Code:
 awk '{if ($2>=0.5) print $1"\t""1"; else if($2<=-0.5) print $1"\t""-1"}'  input |awk '{ sum+=$2} END {print sum}'

Code:
2

Code:
awk '{if ($3>=0.5) print $1"\t""1"; else if($3<=-0.5) print $1"\t""-1"}'  input |awk '{ sum+=$2} END {print sum}'

Code:
0

Code:
awk '{if ($4>=0.5) print $1"\t""1"; else if($4<=-0.5) print $1"\t""-1"}'  input |awk '{ sum+=$2} END {print sum}'

Code:
0

But I have many columns. Is there anyway I can apply the same command looping over all the columns that gives the above output at once? thanx in advance.
# 2  
Old 04-11-2014
Try something like:
Code:
awk '
  NR==1{
    n=split($0,Header)
    next
  }
  {
    for(i=2; i<=NF; i++) {
      if($i>0.5)  Total[i]++
      if($i<-0.5) Total[i]--
    }
  }
  END{
    for(i=2; i<=n; i++)
      print Header[i] ":" Total[i]
  }
' file

Output:
Code:
a1:1
a2:-1
a3:-1

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-11-2014
You have given incorrect output...
you code is comparing for header column and providing the output as 2, 0, 0.
If you go throug that manually, you will get 1, -1, -1 as output.
Below is the code for that

Code:
awk 'NR == 1 {split($0, c, FS); next}
  {if($2 >= 0.5) {v[2] += 1} else if($2 <= -0.5) {v[2] -= 1};
  if($3 >= 0.5) {v[3] += 1} else if($3 <= -0.5) {v[3] -= 1};
  if($4 >= 0.5) {v[4] += 1} else if($4 <= -0.5) {v[4] -= 1}}
  END {for(i = 2; i <= length(c); i++) print c[i] ":" v[i]}' file

# 4  
Old 04-11-2014
yes. you are right. thanx for pointing it out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script, For loop output to excel as columns

Hi, I have a shell script which analyses the log folder for a specific string and throws me the output. I have used for loop since it does this in multiple servers. Now I want to save the output in a excel in the below format. Can someone please help? The output which I get Server1 : count... (14 Replies)
Discussion started by: srilaxman
14 Replies

2. Shell Programming and Scripting

awk script to remove last two columns

I am writing an awk script to remove the last two fields in a pipe delimited file. The script that I have currently deletes the last two fields but each field is being printed on its own line in the output. I can't seem to get the rows to print out on the same line as the input data. This is what I... (4 Replies)
Discussion started by: chamuco666
4 Replies

3. Linux

Problem with my loop and awk script

Sorry if this is a super simple issue, but am extremely new to this and am trying to teach myself as I go along. But can someone please help me out? I have a data file similar to this for many samples, for all chromosomes Sample Chr bp p roh Sample1 1 49598178 0 1... (14 Replies)
Discussion started by: vuvuzelo
14 Replies

4. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

5. Shell Programming and Scripting

awk script: loop through array

I have a large file where I want to extract the data by using awk script. I have made a small sample of the input data. I have in the awk script two condition . The first one is to collect the initial time and the second one to collect the end time. I stored the difference between (Time=end-start)... (8 Replies)
Discussion started by: ENG_MOHD
8 Replies

6. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

Computing the ratio of similar columns in the two files using awk script

Thanks Bartus11 for your help in the following code to compare the two files "t1" and "t2". awk 'NR==FNR{a=1;next}$2 in a{print $2}' t1 t2 First can anyone explain that what is the purpose of assigning a =1? Second, the current script is printing out the matched columns between the... (4 Replies)
Discussion started by: coder83
4 Replies

8. Shell Programming and Scripting

Awk script to replace null columns with blank

I have a file with contents "08011"||20080812 "23348"|20080827|20080924 "23387"|20080829|20080915 "23581"|20081003|20081028 "23748"|20081017|20090114 "24095"|20080919|20081013 "24105"|20070723|20070801 "24118"|20080806|20081013 "24165"|20080820|20080912 "24221"|20080908|20080929 i... (3 Replies)
Discussion started by: sonam273
3 Replies

9. Shell Programming and Scripting

for loop in awk script using ksh

Guys, I am new in awk , I face problem while i try to use for loop in awk, I am using ksh, i am trying to set a for loop which runs as man times as the records in a file , the for loop like for(a=1;a<=5;a++) is working in my awk script but the one i need is not working :wall: for example ... (8 Replies)
Discussion started by: djahmed
8 Replies

10. Shell Programming and Scripting

AWK script to print all the columns excpet the one specified

I have several columns by the name A B C D E...... and I want to print all the column other than column C and D. Could you please help me with the awk script? Thanks!! (3 Replies)
Discussion started by: kn.naresh
3 Replies
Login or Register to Ask a Question

Featured Tech Videos