0

I have a tab-delimited text file containing C1...Cn columns and R1 to Rn rows. Column 10 (C10) has a field 'X'=four comma separated integer values (X=a,b,c,d). I would like do a small arithmetic operation on C10, such as (a/(a+b+c+d))*100 and put the results as new Column (Cn+1) by appending the same file OR in a new out file.

(1) Can anybody help me with a unix shell script to do it for a single file?

(2) If I want loop over the same calculations over multiple files, can you help me with a shell wrapper?

2 Answers 2

1

Use awk method

awk -F'\t' '{ A=$10;gsub(/X=/,"",A); split(A,B,","); print $0"\t"(B[1]/(B[1]+B[2]+B[3]+B[4]))*100 }' input.txt > output.txt
3
  • Thanks. Seems awk rocks. However, within some of the columns in my input file, there are multiple nested fields with an identifier and then separated by semicolon. For example C1...C10...Cn # columns with column name C1 to Cn For eg., in C10, there are multiple fields in each row separated by semicolons. eg.: DP=x,y,z,; DP4=a,b,c,d,; etc. Task: From inputfile.txt, choose Column C10, choose field DP4 and perform arithmetic opertaion (c+d)/(a+b+c+d) -> paste as new column Cn+1 in the original tab delimited file.
    – panbar
    Mar 12, 2016 at 21:12
  • Two Solutions here: awk -F'\t' '{ Z=$10; split(Z,A,";"); for (I in A) { if(A[I]~/DP4/) { gsub(/DP4=/,"",A[I]);split(A[I],B,","); } } print $0"\t"((B[3]+B[4])/(B[1]+B[2]+B[3]+B[4]))*100; }' input.txt and the second solution for the same is: awk -F'\t' '{ Z=$10; gsub(/.*DP4=/,"",Z); gsub(/;.*/,"",Z); split(Z,B,","); print $0"\t"((B[3]+B[4])/(B[1]+B[2]+B[3]+B[4]))*100; }' input.txt Mar 13, 2016 at 16:58
  • does this solve your issue? Mar 14, 2016 at 20:09
0

Perl to the rescue!

perl -nlaF '\t' -e ' my ($A, $B, $C, $D) = split /,/, $F[9];
                     print "$_\t", 100*$A/($A+$B+$C+$D);
                   ' input-file > output-file
  • -n reads the input line by line
  • -l handles the newlines in print.
  • -a splits input on what -F specifies
  • $_ contains the line read from the input
2
  • Sorry, the column C10 contains a field identifier X=a,b,c,d
    – panbar
    Mar 11, 2016 at 18:39
  • @panbar: You can remove it by $A =~ s/.*=//.
    – choroba
    Mar 11, 2016 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .