Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/rc/cgrep

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc
# cgrep - context grep

# NB: /dev/null in list of files to force printing source 
# files when there is just one file supplied

grep -n $* /dev/null |
	sed 's/^(.*):([0-9]*): .*$/\1 \2/' |
		while(loc=`{read}){
			echo $loc(1)^:$loc(2)
			start=`{echo 'if(' $loc(2) ' > 2) print(' $loc(2) ' - 2) else print 0' | hoc}
			tail -n +^$start $loc(1) | sed 's/^/	/; 6q'
		}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.