View previous topic :: View next topic |
Author |
Message |
Phenax l33t
Joined: 10 Mar 2006 Posts: 972
|
Posted: Mon Mar 12, 2007 10:30 pm Post subject: [SCRIPT] icat |
|
|
Quick Bash script I wrote.
It basically does a cat * | grep $1 in the directory and tells you which file it found it in.
$1 = First argument to the script.
Feel free to improve, I'm not really inclined to as it suits me fine at its current state.
Code: |
#!/bin/bash
#Kyle 'Phenax' Hunter
NONE="\e[0m"
BLUE="\e[1;34m"
FORUM="[b][color=blue]"
FORUMEND="[/b][/color]"
keyword=$1
maxdepth=$2
format=$3
function get_files() {
if [[ $maxdepth -gt 0 ]]; then
files=`find . -maxdepth $maxdepth -type f`
else
files=`find . -maxdepth 1 -type f`
fi
files_array=( `echo $files | xargs --null` )
files_array_len=${#files_array[@]}
}
function execute() {
get_files
Y=0
while [ $Y -lt $files_array_len ]; do
if [[ `grep $keyword < ${files_array[$Y]}` ]]; then
if [[ $format == "forum" ]]; then
echo -e "\n${FORUM}${files_array[$Y]}${FORUMEND}"
else
echo -e "\n${BLUE}${files_array[$Y]}${NONE}"
fi
grep -n $keyword < ${files_array[$Y]}
fi
let Y=Y+1
done
}
execute
|
Update 03-21-07:
Remove unneeded cats.
Added maxdepth handling for argument 2.
Added the option for "forum output" for argument 3.
Changed it to `grep -n` ... Line numbers are wonderful.
Last edited by Phenax on Thu Mar 22, 2007 1:39 am; edited 6 times in total |
|
Back to top |
|
|
niceness Tux's lil' helper
Joined: 02 May 2006 Posts: 86 Location: NS, Serbia
|
Posted: Tue Mar 13, 2007 10:57 am Post subject: |
|
|
Isn't this the same as grep -R (output format aside) ?
Code: | $ ./icat /bin/sh
./mount_iso.sh
#!/bin/sh
./print_file_list.sh
#!/bin/sh
./wallsetter
#!/bin/sh
./test.sh
#!/bin/sh
./watch.sh
#!/bin/sh
./imageshack-upload
#!/bin/sh
|
Code: |
$ grep -R /bin/sh .
./mount_iso.sh:#!/bin/sh
./print_file_list.sh:#!/bin/sh
./wallsetter:#!/bin/sh
./test.sh:#!/bin/sh
./watch.sh:#!/bin/sh
./imageshack-upload:#!/bin/sh
|
|
|
Back to top |
|
|
boniek Guru
Joined: 26 Mar 2005 Posts: 373
|
|
Back to top |
|
|
|