Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SCRIPT] icat
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Phenax
l33t
l33t


Joined: 10 Mar 2006
Posts: 972

PostPosted: Mon Mar 12, 2007 10:30 pm    Post subject: [SCRIPT] icat Reply with quote

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
View user's profile Send private message
niceness
Tux's lil' helper
Tux's lil' helper


Joined: 02 May 2006
Posts: 86
Location: NS, Serbia

PostPosted: Tue Mar 13, 2007 10:57 am    Post subject: Reply with quote

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
View user's profile Send private message
boniek
Guru
Guru


Joined: 26 Mar 2005
Posts: 373

PostPosted: Tue Mar 13, 2007 11:21 am    Post subject: Reply with quote

http://sial.org/howto/shell/useless-cat/
_________________
[HOWTO]New freetype subpixel font rendering for lcd monitors
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum