-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectest.sh
87 lines (84 loc) · 1.95 KB
/
spectest.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
#sorry about the bashim. too lazy. its just a test anyway
function get_tail(){
echo $1| rev| cut -d '_' -f 1 | rev
}
function drop_tail(){
l='_'$(get_tail $1)
echo ${1%$l}
}
function parse(){
astack='';
key='a'
iskey=1;
oldifs=$IFS
IFS=$'\v'
for field in $1
do
if [ "$field" = $'\e{' ]
then
astack=$astack'_s';
iskey=1
continue
fi
if [ "$field" = $'\e}' ]
then
astack=$(drop_tail $astack);
key=$(drop_tail $key);
continue
fi
if [ "$field" = $'\e[' ]
then
astack=$astack'_a';
key=$key'_0'
iskey=0;
continue
fi
if [ "$field" = $'\e]' ]
then
astack=$(drop_tail $astack);
key=$(drop_tail $key);
key=$(drop_tail $key);
if [ "$(get_tail $astack)" = 's' ]
then
iskey=1;
fi
continue
fi
if [ $iskey = 1 ]
then
key=$key'_'$field
iskey=0
else
echo $key"="$field
if [ $(get_tail $astack) = 's' ]
then
iskey=1
l=$(echo $key| rev| cut -d '_' -f 1 | rev)
key=${key%'_'$l}
fi
if [ $(get_tail $astack) = 'a' ]
then
l=$(echo $key| rev| cut -d '_' -f 1 | rev)
key=${key%'_'$l}'_'$(expr $l + 1)
fi
fi
done
IFS=$oldifs
}
echo
echo '==='
echo
echo 'json: [ { name:maier,sex:male,cars:[ porsche,benz ] } ]'
str=$'\e[\v\e{\vname\vmaier\vsex\vmale\vcars\v\e[\vporsche\vbenz\v\e]\v\e}\v\e]\v'
echo 'data: '$str
echo 'parsed:'
parse $str
echo
echo '==='
echo
echo 'json: [ bla, bla , bla , bla,bla,[ bla,bla]]'
str=$'\e[\vbla\vbla\vbla\vbla\vbla\v\e[\vbla\vbla\v\e]\v\e]\v'
echo 'data:' $str
echo 'parsed:'
parse $str