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