-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin-using-awk.sh
executable file
·63 lines (63 loc) · 2.19 KB
/
join-using-awk.sh
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
#!/usr/bin/bash
echo -e "\nawk and sort:\n"
time awk -F';' '
# Read dept.csv and store in associative array
FNR==NR {
dept[$1] = $0
#print "Reading dept.csv: Key=" $1 ", Line=" $0 > "/dev/stderr"
next
}
# For each line in employee.csv, join with dept.csv based on the key
{
key = $5
#print "Reading employee.csv: Key=" key ", Line=" $0 > "/dev/stderr"
if (key in dept) {
#print "Match found: Employee Line=" $0 ", Dept Line=" dept[key] > "/dev/stderr"
print $0";"dept[key]
} else {
print "No match for key: " key > "/dev/stderr"
}
}
' <(sed 1d dept.csv | sort -k 1 --field-separator=';') <(sed 1d employee.csv | sort -k 1 --field-separator=';')
echo -e "\nawk and sort\n"
time awk -F';' '
# Read dept.csv and store in associative array
FNR==NR {
dept[$1] = $0
#print "Reading dept.csv: Key=" $1 ", Line=" $0 > "/dev/stderr"
next
}
# For each line in employee.csv, join with dept.csv based on the key
{
key = $5
#print "Reading employee.csv: Key=" key ", Line=" $0 > "/dev/stderr"
if (key in dept) {
#print "Match found: Employee Line=" $0 ", Dept Line=" dept[key] > "/dev/stderr"
print $0";"dept[key]
} else {
print "No match for key: " key > "/dev/stderr"
}
}
' <(sed 1d dept.csv) <(sed 1d employee.csv) | grep -e '.*;.*;.*;.*;0;0;.*'
echo -e "\nThe equivalent of the SQL request:\n"
time awk -F';' '
# Read dept.csv and store in associative array
FNR==NR {
dept[$1] = $0
#print "Reading dept.csv: Key=" $1 ", Line=" $0 > "/dev/stderr"
next
}
# For each line in employee.csv, join with dept.csv based on the key
{
key = $5
#print "Reading employee.csv: Key=" key ", Line=" $0 > "/dev/stderr"
if (key in dept) {
#print "Match found: Employee Line=" $0 ", Dept Line=" dept[key] > "/dev/stderr"
print $0";"dept[key]
} else {
print "No match for key: " key > "/dev/stderr"
}
}
' <(sed 1d dept.csv) <(sed 1d employee.csv) \
| grep -e '.*;.*;.*;.*;0;0;.*' \
| cut -d';' -f2,7