fix _make_git_tar_dir.sh - don't 'append directory' to symlinks
[cmdllinux.git] / scripts / _find_file_section.sh
blob35acb1b767d78428b872f7033ccd871a4dc96fbd
1 #set -x
2 #read file sections - key word to key word
3 #use -- param if -* pattern
4 #sed -n '3,5p' file
5 [ -z "$1" ] && exit 1
6 setparam=1
7 while true; do
8 parm="$1"
9 if [ "$parm" = "-P" ]; then
10 [ -z "$grep_param" ] && grep_param="-P" || grep_param="$grep_param -P"
11 shift
12 continue
13 else
14 setparam=
16 if [ "$parm" = "-I" ]; then
17 [ -z "$grep_param" ] && grep_param="-i" || grep_param="$grep_param -i"
18 shift
19 continue
20 else
21 setparam=
23 if [ "$parm" = "-o" ]; then
24 inv_chkpatt=1
25 shift
26 continue
27 else
28 setparam=
30 if [ "$parm" = "-d" ]; then
31 exclude=1
32 shift
33 continue
34 else
35 setparam=
37 if [ "$parm" = "-u" ]; then
38 check_pattern=1
39 shift
40 continue
41 else
42 setparam=
44 if [ "$parm" = "-c" ]; then
45 close_end=1
46 shift
47 continue
48 else
49 setparam=
51 if [ "$parm" = "-e" ]; then
52 exit_nomatch=1
53 shift
54 continue
55 else
56 setparam=
58 if [ "$parm" = "-x" ]; then
59 longest=1
60 shift
61 continue
62 else
63 setparam=
65 if [ "$parm" = "-q" ]; then
66 dont_print_file=1
67 shift
68 continue
69 else
70 setparam=
72 if [ "$parm" = "-2" ]; then
73 find_next_line=1
74 shift
75 continue
76 else
77 setparam=
79 if [ "$parm" = "-s" ]; then
80 start_line_num=1
81 shift
82 continue
83 else
84 setparam=
86 if [ "$parm" = "-1" ]; then
87 print_wo_last=1
88 shift
89 continue
90 else
91 setparam=
93 if [ "$parm" = "-z" ]; then
94 print_till_end=1
95 shift
96 continue
97 else
98 setparam=
100 if [ "$parm" = "-i" ]; then
101 print_inner=1
102 shift
103 continue
104 else
105 setparam=
107 if [ "$parm" = "-n" ]; then
108 print_num=1
109 shift
110 continue
111 else
112 setparam=
114 #use -- param if -* pattern
115 if [ "$parm" = "--" ]; then
116 shift
117 break
119 if [ -z "$setparam" ]; then
120 if [ "$parm" != "$(echo "$parm" | sed 's%^-%%')" ]; then
121 echo "$0: No such param"
122 exit 1
123 else
124 break
127 done
129 if [ $exclude ]; then
130 excludepatt="$1"
131 [ "$excludepatt" != "${excludepatt#-}" ] && excludepatt="\\$excludepatt"
132 shift
135 if [ $check_pattern ]; then
136 chkpatt="$1"
137 [ "$chkpatt" != "${chkpatt#-}" ] && chkpatt="\\$chkpatt"
138 shift
141 file="$1"
142 [ ! -f "$file" ] && exit 1
144 grep1="$2"
145 [ "$grep1" != "${grep1#-}" ] && grep1="\\$grep1"
147 grep2="$3"
148 [ "$grep2" != "${grep2#-}" ] && grep2="\\$grep2"
150 if [ $start_line_num ]; then
151 if echo "$grep1" | grep -q "[^[:digit:]]"; then
152 echo "$0: Parameter not numeric"
153 exit 1
154 else
155 startnum="$grep1"
159 set -- $grep_param
161 if [ $start_line_num ]; then
162 NUMS="$startnum"
163 else
164 if [ $longest ]; then
165 NUMS=$(grep $@ -n "$grep1" "$file" | cut -d : -f 1 | head -n 1)
166 else
167 NUMS=$(grep $@ -n "$grep1" "$file" | cut -d : -f 1)
171 for i in $NUMS; do
172 if [ $longest ]; then
173 if [ $find_next_line ]; then
174 j=$(expr $i + 1)
175 ENDNUMA=$(tail -n +$j "$file" | grep $@ -n "$grep2" | cut -d : -f 1 | tail -n 1)
176 [ ! -z "$ENDNUMA" ] && ENDNUMA=$(expr $ENDNUMA + 1)
177 else
178 ENDNUMA=$(tail -n +$i "$file" | grep $@ -n "$grep2" | cut -d : -f 1 | tail -n 1)
180 else
181 if [ $find_next_line ]; then
182 j=$(expr $i + 1)
183 ENDNUMA=$(tail -n +$j "$file" | grep $@ -n "$grep2" | cut -d : -f 1 | head -n 1)
184 [ ! -z "$ENDNUMA" ] && ENDNUMA=$(expr $ENDNUMA + 1)
185 else
186 ENDNUMA=$(tail -n +$i "$file" | grep $@ -n "$grep2" | cut -d : -f 1 | head -n 1)
189 #if [ ! -z "$ENDNUMA" ]; then
190 #echo "$i - $ENDNUMA - $(expr $i - 1 + $ENDNUMA)"
191 #else
192 #echo "$i - 1 - $(expr $i - 1 + 1)"
195 if [ $exit_nomatch ]; then
196 [ -z "$ENDNUMA" ] && continue
199 if [ -z "$ENDNUMA" ]; then
200 ENDNUMA=1
201 if ! [ $dont_print_file ]; then
202 if [ $print_num ]; then
203 echo "---$file:$i *"
204 else
205 echo "---$file *"
208 else
209 if ! [ $dont_print_file ]; then
210 if [ $print_num ]; then
211 echo "---$file:$i"
212 else
213 echo "---$file"
218 if [ "$ENDNUMA" = "1" ]; then
219 if [ $print_till_end ]; then
220 if [ $print_inner ]; then
221 if [ $check_pattern ]; then
222 if [ $inv_chkpatt ]; then
223 if ! tail -n +$(expr $i + 1) "$file" | grep $@ -q "$chkpatt"; then
224 if [ $exclude ]; then
225 tail -n +$(expr $i + 1) "$file" | grep $@ -v "$excludepatt"
226 else
227 tail -n +$(expr $i + 1) "$file"
230 else
231 if tail -n +$(expr $i + 1) "$file" | grep $@ -q "$chkpatt"; then
232 if [ $exclude ]; then
233 tail -n +$(expr $i + 1) "$file" | grep $@ -v "$excludepatt"
234 else
235 tail -n +$(expr $i + 1) "$file"
239 else
240 if [ $exclude ]; then
241 tail -n +$(expr $i + 1) "$file" | grep $@ -v "$excludepatt"
242 else
243 tail -n +$(expr $i + 1) "$file"
246 #tail -n +$(expr $i + 1) "$file"
247 else
248 if [ $check_pattern ]; then
249 if [ $inv_chkpatt ]; then
250 if ! tail -n +$i "$file" | grep $@ -q "$chkpatt"; then
251 if [ $exclude ]; then
252 tail -n +$i "$file" | grep $@ -v "$excludepatt"
253 else
254 tail -n +$i "$file"
257 else
258 if tail -n +$i "$file" | grep $@ -q "$chkpatt"; then
259 if [ $exclude ]; then
260 tail -n +$i "$file" | grep $@ -v "$excludepatt"
261 else
262 tail -n +$i "$file"
266 else
267 if [ $exclude ]; then
268 tail -n +$i "$file" | grep $@ -v "$excludepatt"
269 else
270 tail -n +$i "$file"
273 #tail -n +$i "$file"
275 else
276 if [ $check_pattern ]; then
277 if [ $inv_chkpatt ]; then
278 if ! tail -n +$i "$file" | head -n 1 | grep $@ -q "$chkpatt"; then
279 if [ $exclude ]; then
280 tail -n +$i "$file" | head -n 1 | grep $@ -v "$excludepatt"
281 else
282 tail -n +$i "$file" | head -n 1
285 else
286 if tail -n +$i "$file" | head -n 1 | grep $@ -q "$chkpatt"; then
287 if [ $exclude ]; then
288 tail -n +$i "$file" | head -n 1 | grep $@ -v "$excludepatt"
289 else
290 tail -n +$i "$file" | head -n 1
294 else
295 if [ $exclude ]; then
296 tail -n +$i "$file" | head -n 1 | grep $@ -v "$excludepatt"
297 else
298 tail -n +$i "$file" | head -n 1
301 #tail -n +$i "$file" | head -n 1
302 #sed -n "${i}p" "$file"
304 else
305 if [ $print_inner ]; then
306 if [ $check_pattern ]; then
307 if [ $inv_chkpatt ]; then
308 if ! tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2) | grep $@ -q "$chkpatt"; then
309 if [ $exclude ]; then
310 tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2) | grep $@ -v "$excludepatt"
311 else
312 tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2)
315 else
316 if tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2) | grep $@ -q "$chkpatt"; then
317 if [ $exclude ]; then
318 tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2) | grep $@ -v "$excludepatt"
319 else
320 tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2)
324 else
325 if [ $exclude ]; then
326 tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2) | grep $@ -v "$excludepatt"
327 else
328 tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2)
331 #tail -n +$(expr $i + 1) "$file" | head -n $(expr $ENDNUMA - 2)
332 else
333 if [ $print_wo_last ]; then
334 if [ $check_pattern ]; then
335 if [ $inv_chkpatt ]; then
336 if ! tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1) | grep $@ -q "$chkpatt"; then
337 if [ $exclude ]; then
338 tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1) | grep $@ -v "$excludepatt"
339 else
340 tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1)
343 else
344 if tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1) | grep $@ -q "$chkpatt"; then
345 if [ $exclude ]; then
346 tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1) | grep $@ -v "$excludepatt"
347 else
348 tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1)
352 else
353 if [ $exclude ]; then
354 tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1) | grep $@ -v "$excludepatt"
355 else
356 tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1)
359 #tail -n +$i "$file" | head -n $(expr $ENDNUMA - 1)
360 else
361 if [ $check_pattern ]; then
362 if [ $inv_chkpatt ]; then
363 if ! tail -n +$i "$file" | head -n $ENDNUMA | grep $@ -q "$chkpatt"; then
364 if [ $exclude ]; then
365 tail -n +$i "$file" | head -n $ENDNUMA | grep $@ -v "$excludepatt"
366 else
367 tail -n +$i "$file" | head -n $ENDNUMA
370 else
371 if tail -n +$i "$file" | head -n $ENDNUMA | grep $@ -q "$chkpatt"; then
372 if [ $exclude ]; then
373 tail -n +$i "$file" | head -n $ENDNUMA | grep $@ -v "$excludepatt"
374 else
375 tail -n +$i "$file" | head -n $ENDNUMA
379 else
380 if [ $exclude ]; then
381 tail -n +$i "$file" | head -n $ENDNUMA | grep $@ -v "$excludepatt"
382 else
383 tail -n +$i "$file" | head -n $ENDNUMA
386 #tail -n +$i "$file" | head -n $ENDNUMA
390 done
392 [ $close_end ] && echo "+++"