Welcome, guest | Sign In | My Account | Store | Cart

look for a text pattern in files defined by a pattern. First argument is passed to the find command, second one to the grep

./find_n_grep.sh '*.py' 'dict'

more over, it sort by date.

Bash, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/usr/bin/env zsh

tmp_file="/tmp/_unsorted_find_n_grep"
echo "--- find <$2> in <$1>" > $tmp_file

find . -follow -iname "$1" | while read file
do
	timestamp=$(ls -l --time-style=+"%Y-%m-%d %H:%M:%S" "$file" | gawk '{print $6, $7}')
	linestack=${(@f)$(egrep "$2" "$file")}
	for line in $linestack 
	do
		echo "$timestamp $file: $line" >> $tmp_file
	done
done

cat $tmp_file | sort
rm $tmp_file
Created by yota on Tue, 17 Sep 2013 (GPL3)
Bash recipes (41)
yota's recipes (13)

Required Modules

  • (none specified)

Other Information and Tasks

  • Licensed under the GPL 3
  • Viewed 5773 times
  • Revision 2 (updated 10 years ago)