Sunday, September 23, 2012

Search inside text files

When we have many text files such as code samples or research documents then more often than not we want something which is in one of the files but we cannot remember which one. This is when searching inside text files becomes necessary. The great thing about the following script that I wrote is that it can search all sub-directories recursively.

I plan to add switches to ignore sub-directories and specific files in a later version.

# search inside text files : v2.0 by Jones

for i in `find . -type f -name "*"`
#echo $i
#cat "$i" | head -n 1

if [ "`cat $i | grep $1`" ] ; 
 echo "-------------------------------------" 
 echo "FILE : " $i 
 grep $1 $i -A2 -B2 --color=auto


No comments:

Print chess board in command line

The following bash one-liner will print a chess board in a terminal (the script works for the shells bash and ksh only) for (( i = 1; i ...