-
Notifications
You must be signed in to change notification settings - Fork 2
/
vlookup.awk
63 lines (60 loc) · 1.15 KB
/
vlookup.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
53
54
55
56
57
58
59
60
61
62
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
BEGIN{
FS=","
}
FNR==NR{
if(FNR==1){
if(C1 !~ /^[0-9]+$/ || C3 !~ /^[0-9]+$/){
for(i=1;i<=NF;i++){
if(trim(tolower($i))==trim(tolower(C1))){
C1=i
if(C3 ~ /^[0-9]+$/)
break
}
else if(trim(tolower($i))==trim(tolower(C3))){
C3=i
if(C1 ~ /^[0-9]+$/)
break
}
}
if(i>NF){
if(C1 !~ /^[0-9]+$/)
print "No matching column found for "C1". Program will exit now..."
if(C3 !~ /^[0-9]+$/)
print "No matching column found for "C3". Program will exit now..."
exit 1
}
}
}
if($C1 in a){
print "Non-unique values found in lookup file. Please correct this. Program will exit now"
print $C1
exit 1
}
a[$C1]=$C3
next
}
FNR==1{
if(!(C2 ~ /^[0-9]+$/)){
for(i=1;i<=NF;i++){
if($i==C2){
C2=i
break
}
}
if(i>NF){
print "No matching column found for "C2". Program will exit now..."
exit 1
}
}
print $0","toupper(C6) > C4
next
}
($C2 in a){
print $0","a[$C2] >> C4
}
!($C2 in a){
print $0 >> C5
}