Skip to content

Commit

Permalink
grep script use sed
Browse files Browse the repository at this point in the history
  • Loading branch information
soarpenguin committed Dec 10, 2013
1 parent bbf39e7 commit d3f77a6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions bashrc/grep3c.sh
@@ -0,0 +1,35 @@
#!/bin/env bash

# if there is only one argument, exit

case $# in
1);;
*) echo "Usage: $0 pattern";exit;;
esac;

# again - I hope the argument doesn't contain a /

sed -n '
'/$1/' !{
# put the non-matching line in the hold buffer
h
}
'/$1/' {
# found a line that matches
# add the next line to the pattern space
N
# exchange the previous line with the
# 2 in pattern space
x
# now add the two lines back
G
# and print it.
p
# add the three hyphens as a marker
a\
---
# remove first 2 lines
s/.*\n.*\n\(.*\)$/\1/
# and place in the hold buffer for next time
h
}'

0 comments on commit d3f77a6

Please sign in to comment.