-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini_edit
executable file
·64 lines (54 loc) · 906 Bytes
/
ini_edit
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
#!/bin/bash
set -euo pipefail
usage() {
printf -- "%s : usage : %s source-ini key1 key2 -p regex-key key 3" "$0" "$0"
}
regexs=()
args=()
list=()
dryRun=false
while (("$#")); do
case "$1" in
-p | --regex | --perl-regex)
shift
regexs+=("$1")
;;
-n | --dry-run)
dryRun=true
;;
--help | -h)
usage
exit 0
;;
--)
shift
args+=("$@")
;;
*)
args+=("$1")
;;
esac
shift
done
set -- "${args[@]}"
source=$1
shift
dest=$1
shift
if test "$#" -gt 0; then
list+=("$@")
fi
for regex in "${regexs[@]}"; do
mapfile -t newKeys < <(grep -Po -- "[^=]*${regex}[^=]*=" "$dest")
for newKey in "${newKeys[@]}"; do
list+=("${newKey%=}")
done
done
if $dryRun; then
useSed="echo sed"
else
useSed="sed"
fi
for seditem in "${list[@]}"; do
$useSed -r "s#${seditem}=.*#$(grep -E -- "${seditem}=" "$source" | sed 's/#/\#/')#" -i "$dest"
done