Skip to content

Commit d3f77a6

Browse files
committed
grep script use sed
1 parent bbf39e7 commit d3f77a6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

bashrc/grep3c.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/env bash
2+
3+
# if there is only one argument, exit
4+
5+
case $# in
6+
1);;
7+
*) echo "Usage: $0 pattern";exit;;
8+
esac;
9+
10+
# again - I hope the argument doesn't contain a /
11+
12+
sed -n '
13+
'/$1/' !{
14+
# put the non-matching line in the hold buffer
15+
h
16+
}
17+
'/$1/' {
18+
# found a line that matches
19+
# add the next line to the pattern space
20+
N
21+
# exchange the previous line with the
22+
# 2 in pattern space
23+
x
24+
# now add the two lines back
25+
G
26+
# and print it.
27+
p
28+
# add the three hyphens as a marker
29+
a\
30+
---
31+
# remove first 2 lines
32+
s/.*\n.*\n\(.*\)$/\1/
33+
# and place in the hold buffer for next time
34+
h
35+
}'

0 commit comments

Comments
 (0)