-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandleCSV.bats
More file actions
executable file
·74 lines (69 loc) · 1.63 KB
/
handleCSV.bats
File metadata and controls
executable file
·74 lines (69 loc) · 1.63 KB
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
#!/usr/bin/env bats
#The handleCSV function parses the CSV input in the file
#named by its argument. Each line of the CSV input file consists of three fields
#separated by commas. The function removes leading and trailing whitespace
#around each field, and #outputs each field on its own line preceding the
#field by either 1), 2), or 3). Each ) should be followed by exactly one space.
#For example, if the input is:
#
#a, b, c
#e,f,g
#aaa,bbb,ccc
#hi, there, howdy
#
#then the output of the function is:
#
#1) a
#2) b
#3) c
#1) e
#2) f
#3) g
#1) aaa
#2) bbb
#3) ccc
#1) hi
#2) there
#3) howdy
handleCSV() {
if [ "$#" -ne 1 ] || ! [ -f "$1" ]; then
return 1
fi
cat "$1" | tr "," "\n" | sed -r 's/(\s)+(.*)/\2\1/' > new.txt
var=1
while read p; do
echo "$var) $p"
var=$((var + 1))
if [ $var -eq 4 ];then
var=1
fi
done <new.txt
return 0
}
@test "handleCSV CSVinput" {
run handleCSV CSVinput
#$lines is an array of the output of the run
[ "${lines[0]}" = "1) a" ]
[ "${lines[1]}" = "2) b" ]
[ "${lines[2]}" = "3) c" ]
[ "${lines[3]}" = "1) e" ]
[ "${lines[4]}" = "2) f" ]
[ "${lines[5]}" = "3) g" ]
[ "${lines[6]}" = "1) aaa" ]
[ "${lines[7]}" = "2) bbb" ]
[ "${lines[8]}" = "3) ccc" ]
[ "${lines[9]}" = "1) hi" ]
[ "${lines[10]}" = "2) there" ]
[ "${lines[11]}" = "3) howdy" ]
[ "$status" = "0" ]
}
#test without no arguments; should return 1
@test "handleCSV" {
run handleCSV
[ "$status" = "1" ]
}
#test without bad argument; should return 1
@test "handleCSV nofile" {
run handleCSV nofile
[ "$status" = "1" ]
}