-
Notifications
You must be signed in to change notification settings - Fork 0
/
spread-regex
executable file
·38 lines (32 loc) · 939 Bytes
/
spread-regex
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
#!/bin/sh
if [ "$#" -lt "2" ]; then
echo 'You need to provide two parameters: the amount of chars to allow ' \
'between each searched char, and the string to sperad out. The amount ' \
'may be negative, in which case the search string is reversed and the ' \
'rest of the search continues as usual.' >&2
exit 1
fi
# $1 - how many chars to allow between each searched char
amount="$1"
shift
if ! { echo "$amount" | grep '^[0-9-][0-9]*$' >/dev/null 2>&1; }; then
echo 'The amount must be a number' >&2
exit 1
fi
# $2 - search string to spread out
search="$1"
if [ "$amount" -lt 0 ]; then
amount=$((0-amount))
search="$(echo "$search" | awk '
{
for (i=length; i!=0; i--) {
rev=rev substr($0,i,1) # append one char to string rev
}
}
END {
print rev
}')"
fi
echo "$search" \
| sed -e 's/./&.\\{0,'"$amount"'\\}/g' \
| sed -e 's/\.\\{0,'"$amount"'\\}$//g'