-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit-file
executable file
·115 lines (90 loc) · 1.85 KB
/
edit-file
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
function appendHelp {
echo "Syntax: edit-file <file> <search/placement> [placement]"
}
function appendLine {
FILE="$1"
SEARCH="$2"
PLACE="$3"
echo SEARCH: "$SEARCH"
echo FILE: "$FILE"
echo PLACEMENT: "$PLACE"
if [ -z "$SEARCH" ]; then
appendHelp
exit 1
fi
if [ -z "$FILE" ]; then
appendHelp
exit 1
fi
if [ -z "$PLACE" ]; then
PLACE="$SEARCH"
fi
echo Try append "$SEARCH" to file "$FILE"
if [ -f "$FILE" ]; then
grep -qxF "$SEARCH" "$FILE" || echo "$PLACE" >> "$FILE"
else
echo create file "$FILE"
echo "$PLACE" > "$FILE"
fi
}
function replaceHelp {
echo "Syntax: edit-file <file> <search> <replacement>"
}
function replaceLine {
FILE="$1"
SEARCH="$2"
PLACE="$3"
echo SEARCH: "$SEARCH"
echo FILE: "$FILE"
echo PLACEMENT: "$PLACE"
if [ -z "$SEARCH" ]; then
replaceHelp
exit 1
fi
if [ -z "$FILE" ]; then
replaceHelp
exit 1
fi
if [ -z "$PLACE" ]; then
replaceHelp
exit 1
fi
echo Try replace "$SEARCH" in file "$FILE"
if [ -f "$FILE" ]; then
sed -i "s|$SEARCH|$PLACE|g" "$FILE"
fi
}
function removeHelp {
echo "Syntax: edit-file remove <file> <search>"
}
function removeLine {
FILE="$1"
SEARCH="$2"
if [ -z "$SEARCH" ]; then
replaceHelp
exit 1
fi
if [ -z "$FILE" ]; then
replaceHelp
exit 1
fi
echo Try remove "$SEARCH" in file "$FILE"
if [ -f "$FILE" ]; then
sed -i "s|$SEARCH||g" "$FILE"
fi
}
case $1 in
append)
appendLine "$2" "$3" "$4"
;;
replace)
replaceLine "$2" "$3" "$4"
;;
remove)
removeLine "$2" "$3"
;;
*)
echo $"Usage: edit-file {append|replace|remove} <file> Options..."
exit 1
esac