annotate column_extract.sh @ 0:e77c9484b2d0 draft default tip

Uploaded
author saskia-hiltemann
date Thu, 22 Oct 2015 09:18:30 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
1 #!/bin/bash
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
2
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
3 inputfile=$1
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
4 outputfile=$2
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
5 removeheader=$3
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
6 columns="$@"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
7
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
8 cols="${columns// /,}" #make comma-separated
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
9
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
10 #skip first three arguments
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
11 firstcomma=`expr index "$cols" ,`
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
12 cols="${cols:$firstcomma}"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
13 secondcomma=`expr index "$cols" ,`
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
14 cols="${cols:$secondcomma}"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
15 thirdcomma=`expr index "$cols" ,`
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
16 cols="${cols:$thirdcomma}"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
17 cols="${cols//:/}" #remove colons
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
18 echo "colums to print: $cols"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
19
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
20 arr=$(echo $cols | tr "," "\n")
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
21
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
22 for x in $arr
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
23 do
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
24 echo $x
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
25 done
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
26
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
27 myArray=($columns)
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
28 i=3
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
29 len=${#myArray[@]}
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
30 mycols=""
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
31 echo "len: $len"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
32 while [ $i -le $len ]
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
33 do
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
34 echo "myarray: ${myArray[$i]}"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
35 mycols+=${myArray[$i]}
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
36 i=$[$i+2]
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
37 done
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
38 mycols="${mycols//:/,}" #make comma-separated
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
39 mycols="${mycols%?}"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
40 echo "mycols: $mycols"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
41
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
42 awk 'BEGIN{
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
43 FS="\t";
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
44 OFS="\t";
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
45 columns="'"$mycols"'";
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
46 len=split(columns,arr,",")
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
47 }{
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
48 if (index($1,"#")==1 || $1==""){ #print header as--s
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
49 if("'"$removeheader"'"=="N"){
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
50 print $0
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
51 }
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
52 }
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
53 else{
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
54 for (i=1;i<len;i++){
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
55 j=arr[i]
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
56 printf $j"\t"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
57 }
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
58 j=arr[len]
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
59 printf $j"\n"
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
60 }
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
61 }END{
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
62
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
63 }' $inputfile > $outputfile
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
64
e77c9484b2d0 Uploaded
saskia-hiltemann
parents:
diff changeset
65