-
Notifications
You must be signed in to change notification settings - Fork 0
/
520.sh
61 lines (34 loc) · 926 Bytes
/
520.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
#!/bin/bash
# cgrep-- 顯示前後文並且符合模板的全域搜索和列印指令
context=0
esc="^["
boldon="${esc}[1m"
boldoff="${esc}[22m"
tempout='/tmp/cgrep.$$'
sedscript='/tmp/cgrep.sed.$$'
function MatchOrNot
{
matches=0
echo "s/$pattern/${boldon}$pattern${boldoff}/g" >$sedscript
for lineon in $(grep -n "$pattern" $1 | cut -d: -f1) #(a) -f1
do
if [ $context -gt 0 ]; then
prev="$(( $lineon - $context))"
if [ $prev -lt 1]; then
prev="1"
fi
next="(( $lineon + $context ))"
if [ $matches -gt 0]; then
echo "${prev}i\\" >> $sedscript #(b) i\\
fi
echo "${prev,${next}p}" >> $sedscript
else
echo "${lineon}p" >> $sedscript #(c) >>, p
fi
matches="$(( matches +1 ))"
done
if [$matches -gt 0]; then
sed -n -f $sedscript $1 | uniq | more #(d) -f
fi
}
exit 0