forked from rohitprajapati/toyeca-cutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toyeca-cutter.awk
executable file
·52 lines (45 loc) · 1.32 KB
/
toyeca-cutter.awk
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
BEGIN { #Override comma as a separator with -F
if ( FS==" ") {
FS=","
}
}
NR==1 { #First line is the headers for the columns
split(c, ca, ",")
#Create Associative Array with the headers we want
for (i = 1 ; i <= length(ca) ; i++) {
gsub(/ /, "", ca[i])
cm[ca[i]] = 1
}
#Create Associative Array with the column numbers we want
j=1
for (i = 1 ; i <= NF ; i++) {
if (cm[$i] == 1) {
ce[$i] = i
j++
}
}
#Create array with column numbers in order provided on command line
j=1
for (i in ca ) {
cc[j]=ce[ca[i]]
j++
}
#Bail if no headers matched.
if (length(cc) == 0) {
print "No headers matched."
exit 1
}
}
#For all lines
{
ci = "" #Start with 1st field
#Print needed Fields
for (i in cc ) {
if (ci == "") { #First colum
ci = $(cc[i]) #Just add the column
} else { #otherwise
ci = ci FS $(cc[i]) #Add a separator and the new column
}
}
print ci #Print what is left
}