summaryrefslogtreecommitdiffstats
path: root/funtools/funtbl.tmpl
blob: c230c0e028f747c4154b777eb4950f50572f4fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/sh

EXE=AWK
N=1
FILE=-
DOHEAD=0
DORDB=0
XHEAD=""
SEP=" "

# process args
while [ x"$1" != x ]; do
    case $1 in
	-c) shift
	    COLS=$1
            shift
            continue;;
        -help|--help) 
	    echo "usage:"
	    echo " "
	    echo "  funtbl [switches] input_file"
	    echo "  funtbl [switches] < input_file"
	    echo " "
	    echo "switches (optional):"
	    echo " "
	    echo "  -c \"col1 ...\"    # output columns numbers (def: all)"
	    echo "  -h               # prepend column names as a header"
	    echo "  -H prefix        # prepend prefix and column names"
	    echo "  -n num           # extract the nth data table (def: 1)"
	    echo "  -p prog          # awk program (def: host-specific)"
	    echo "  -s sep           # output separator (def: space)"
	    echo "  -T               # output rdb table format"
	    echo " "
	    echo "  -help            # print this message"
	    echo " "
	    echo "examples:"
	    echo " "
	    echo "  funcnts -rs ... | funtbl -c \"1 3\" -n 4 -s \"\t\""
	    echo "  funtbl -c \"1 3\" -n 4 -h -s \"\t\" foo.out"
	    echo "  funtbl -c \"1 3\" -n 4 -T foo.out"
	    echo " "
	    echo "All 3 will output the first and third columns of the fourth"
	    echo "table, with the columns separated by a tab. The second form"
	    echo "will output a single line header of column names. The third"
	    echo "form will output 2-line rdb header."
	    echo " "
            exit 0;;
        -h) DOHEAD=1
	    XHEAD="# "
            shift
            continue;;
        -H) DOHEAD=1
	    shift
	    XHEAD=$1
            shift
            continue;;
        -n) shift
	    N=$1
            shift
            continue;;
        -p) shift
	    EXE=$1
            shift
            continue;;
        -s) shift
	    SEP=$1
            shift
            continue;;
        -T) DORDB=1
	    DOHEAD=1
	    SEP="\t"
            shift
            continue;;
	*)  FILE=$1
            shift
            continue;;
    esac
done

cat $FILE |	\
$EXE '

BEGIN{
    I=0
    STATE=0
    N='"$N"'
    COLS="'"$COLS"'"
    DOHEAD='"$DOHEAD"'
    DORDB='"$DORDB"'
    SEP="'"$SEP"'"
    XHEAD="'"$XHEAD"'"
    if( COLS == "" ){
        NCOL=0
    }
    else{
	NCOL=split(COLS,COLNAMES);
    }
}

# state 0: looking for nth header
STATE==0{
    last = cur
    cur = $0
    dashes=0
    for(j=1; j<=NF; j++){
	if( $j ~ /--*/ ){
	    dashes++;
	}
        else{
            break;
	}
    }
    if( (dashes >= 1) && (dashes == NF) ){
        I=I+1
        if( I==N ){
            STATE=1    
	    LINES=0
	    if( DOHEAD == 1 ){
	        split(last, HEADER)
	        split($0, DASHES)
	    }
        }
    }
}

# look for blank line to end row output
$0 ~ /^$/{
    if( STATE == 1 ){
        STATE=2
    }
}

# state 1: output this line of data
STATE==1{
    # output header, if necessary
    if( LINES == 0 ){
	if( DOHEAD == 1 ){
 	    # output as comment if not rdb
            if( DORDB != 1 ){
	        printf("%s", XHEAD)
	    }
	    # specific columns
	    if( COLS == "" ){
	        for(i=1; i<=NF; i++){
		    printf("%s", HEADER[i])
		    if( i != NF )
		        printf("%s", SEP)
	        }    
                printf("\n")
                if( DORDB == 1 ){
	            for(i=1; i<=NF; i++){
		        printf("%s", DASHES[i])
		        if( i != NF )
		            printf("%s", SEP)
		    }
                    printf("\n")
	        }    
	    }
 	    # all columns
	    else{
	        for(i=1; i<=NCOL; i++){
		    printf("%s", HEADER[COLNAMES[i]])
		    if( i != NCOL ){
		        printf("%s", SEP)
		    }
	        }    
                printf("\n")
                if( DORDB == 1 ){
	            for(i=1; i<=NCOL; i++){
		        printf("%s", DASHES[COLNAMES[i]])
  		        if( i != NCOL ){
		            printf("%s", SEP)
		        }
		    }
                    printf("\n")
                }
	    }    
	}
    }
    else{
	if( COLS == "" ){
	    for(i=1; i<=NF; i++){
		printf("%s", $i)
		if( i != NF )
		    printf("%s", SEP)
	    }    
            printf("\n")
	}
	else{
	    split($0, ARGS) 		
	    for(i=1; i<=NCOL; i++){
		printf("%s", ARGS[COLNAMES[i]])
		if( i != NCOL )
		    printf("%s", SEP)
	    }    
            printf("\n")
	}
    }
    LINES=LINES+1
}
'