-
Notifications
You must be signed in to change notification settings - Fork 3
/
textfileops.sh
202 lines (175 loc) · 5.31 KB
/
textfileops.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Provides the following functions to manipulate text files:
# - Add a section
# - Delete a section
# - Get all entries in a section
# - Add an entry in a section
# - Remove an entry from a section
SECTION_START_PREFIX='#START:'
SECTION_END_PREFIX='#END:'
# Add a section.
# $1 : File
# $2 : Section name
add_section() {
is_section_present $1 $2
if [ $? -eq 0 ]; then
echo "Section $2 already exists"
return 1
fi
local header=$(section_header $2)
local footer=$(section_footer $2)
printf "\n\n$header\n$footer\n" >> $1
return 0
}
# Add a section.
# $1 : File
# $2 : Section name
delete_section() {
is_section_present $1 $2
if [ $? -ne 0 ]; then
echo "Section $2 not found"
return 1
fi
local header=$(section_header $2)
local footer=$(section_footer $2)
sed -r -i "/$header/,/$footer/ d" $1
return 0
}
# Insert a line at the top of a section
# $1: File
# $2: Section name
# $3: Line to insert
insert_top_of_section() {
is_section_present $1 $2
if [ $? -ne 0 ]; then
echo "Section $2 not found"
return 1
fi
local header=$(section_header $2)
# The \\$3 is because if $3 starts with whitespaces but there is no \ before it, those whitespaces are not written to file.
# The double backslash is because a single backslash is escape character for bash. We want to escape the backslash itself so
# that it gets passed as a backslash to sed.
sed -r -i "/$header/ a \\$3" $1
return 0
}
# Insert a line at the bottom of a section
# $1: File
# $2: Section name
# $3: Line to insert
insert_bottom_of_section() {
is_section_present $1 $2
if [ $? -ne 0 ]; then
echo "Section $2 not found"
return 1
fi
local footer=$(section_footer $2)
# The \\$3 is because if $3 starts with whitespaces but there is no \ before it, those whitespaces are not written to file.
# The double backslash is because a single backslash is escape character for bash. We want to escape the backslash itself so
# that it gets passed as a backslash to sed.
sed -r -i "/$footer/ i \\$3" $1
return 0
}
# Replace matching lines inside a section.
# $1: File
# $2: Section name
# $3: Pattern to match
# $4: Replacement line
replace_line() {
is_section_present $1 $2
if [ $? -ne 0 ]; then
echo "Section $2 not found"
return 1
fi
local header=$(section_header $2)
local footer=$(section_footer $2)
# Important: The 'c $4' portion has to be the last thing on its line, and rest of the command has to be on newline.
# Otherwise, it throws an "unmatched {" error.
# The \\$4 is because if $4 starts with whitespaces but there is no \ before it, those whitespaces are not written to file.
# The double backslash is because a single backslash is escape character for bash. We want to escape the backslash itself so
# that it gets passed as a backslash to sed.
#sed -r -i "/$header/,/$footer/ {/$3/ c \\$4
# }" $1
# The /$3/ is changed to \|$3| - ie, the sed delimiter is changed from / to |
# because $3 may itself contain / (slashes)
sed -r -i "/$header/,/$footer/ {\|$3| c \\$4
}" $1
}
# Checks if matching line is present, and accordingly replaces or inserts.
# $1: File
# $2: Section name
# $3: Search for existing line
# $4: Line to insert or replace
insert_or_replace_in_section() {
local contents
contents=$(get_section $1 $2)
echo "$contents" | grep -q "$3"
local found=$?
if [ $found -eq 1 ]; then
echo "$3 not found in $1 section $2. inserting"
insert_bottom_of_section $1 $2 "$4"
else
echo "$3 found in $1 section $2. replacing"
replace_line $1 $2 "$3" "$4"
fi
}
# Replaces a section completely with contents of another file.
# $1 : Target file
# $2 : Section name
# $3 : File to include into target file
replace_section_with_file() {
# Add section if it's not already present.
add_section $1 $2
local header=$(section_header $2)
local footer=$(section_footer $2)
# This sed means "between header and footer, print header, followed by contents of $3 file, then print footer, and delete
# every line between header and footer.
# The "r $3" has to be the last thing on its line to avoid "unmatched {" error
sed -r -i "/$header/,/$footer/ {/$header/ {p; r $3
}; /$footer/p; d}" $1
}
# Delete matching lines inside a section
# $1: File
# $2: Section name
# $3: Pattern to delete
delete_line() {
is_section_present $1 $2
if [ $? -ne 0 ]; then
echo "Section $2 not found"
return 1
fi
local header=$(section_header $2)
local footer=$(section_footer $2)
# Use | (pipe) as delimiter of search string, because it may be a file path with forward
# slashes. To use pipe as delimier, it should be escaped with a \ (backslash).
sed -r -i "/$header/,/$footer/ {\|$3| d}" $1
}
# Gets all lines inside a section, excluding the section header and footer.
# $1: File
# $2: Section name
get_section() {
is_section_present $1 $2
if [ $? -ne 0 ]; then
return 1
fi
local header=$(section_header $2)
local footer=$(section_footer $2)
# -n means don't print the entire search space, but only matched lines.
sed -r -n "/$header/,/$footer/ {/$header/d;/$footer/d; p}" $1
}
# Check if a section already exists
# $1 : File
# $2 : Section name
is_section_present() {
local sechdr=$(section_header $2)
grep -q $sechdr $1
return $?
}
# Form the section start header.
# $1 : Section name
section_header() {
printf "$SECTION_START_PREFIX$1\n"
}
# Form the section end header.
# $1 : Section name
section_footer() {
printf "$SECTION_END_PREFIX$1\n"
}