Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 12th, 2011, 1:05 AM   #1
Kiah07
Newbie
 
Join Date: Jun 2011
Posts: 28
Rep Power: 0 Kiah07 is on a distinguished road
Pass Arguments to GREP from script

I've been working on a script that asks a user what they would like to search for. If the user enters 3 words to search for, I would like to pass those to grep.

##main loops and creates a new variable for each new search string 

exit_timer=0
cycle=0
main  

main() {
while [ $exit_timer == "0" ]
    do
          let "cycle +=1"
                     echo "Please type a string to search for" 
                     read  first_string$cycle
                          echo "Search for an additional string  (y/n)?" 
                          read decision
                          if [ "$decision" == "n" ]; 
                          then  let "exit_timer =1"
fi
done
get_directory
}

Obviously I omitted some things for readability
The problem is, how do i have the script change the grep to reflect how many search terms were entered? If I hard code variables into the grep command, there may be too few or too many.

search() {
grep  -ao "....possibly multiple words....\|....possibly multiple words...." ./
}

Thanks for looking,
Kiah07 is offline   Reply With Quote
Old Jun 12th, 2011, 10:38 PM   #2
Kiah07
Newbie
 
Join Date: Jun 2011
Posts: 28
Rep Power: 0 Kiah07 is on a distinguished road
Cool Re: Pass Arguments to GREP from script

Solved
The exact problem was that a user would imput an unknown number of queries.
The queries were handled by an array.
The array was fed into the grep command.
The output of the array was in the wrong form: red blue green
I needed array output to be in this form ....red....\|....blue....\|....green....\|

So I add characters between with awk. The backslash was tricky because of the escape character in bash. I had to use three to print out 1
awk 'BEGIN { OFS=".....\\\|....." } { $1=$1; print "...."$0"...." ; }'
Here is my script if anyones can use it. It uses grep but adds some user friendliness to multiple search strings and context surrounding the word(s).

#!/bin/bash
clear
echo By:  Kiah07 2011 
echo

#Take multiple search strings from a user and feed them into grep to find file 
#matches regardless of the text or lack thereof surrounding the specified strings. #Just a plaything,


txt=$(tput setaf 3)
txt2=$(tput setaf 1)
trst=$(tput sgr0)

exit_timer=0
cycle=0
declare -a search_array



main() {
while [ $exit_timer == "0" ]
    do
       let "cycle +=1"
       echo "Please type a string to search for>"   
       echo -n $txt"Item $cycle:  $trst"  & read search_array[$cycle]  
             echo 
             echo -n "Search for an additional string?  (y/n)"" "     
             read decision
             echo
                 if [ "$decision" == "n" ]; 
                   then  let "exit_timer =1"
                 fi
    done
parse
}



parse() {
parsed_long_search=`echo ${search_array[*]} | awk 'BEGIN { OFS=".....\\\|....." } { $1=$1; print "...."$0"...." ; }'`  
parsed_short_search=` echo ${search_array[*]} | awk 'BEGIN { OFS="\\\|" } { $1=$1; print ; }'`
get_directory
}



get_directory() {
echo  "What directory should be searched?  **Default is current directory"
read searchdirectory
echo "About to search for:"  $txt${search_array[*]}$trst;
confirm
}



confirm() {
echo -n "Proceed?  (y/n)"" "
 read confirm_answer 
    if [ "$confirm_answer" == "y" ];
    then search
    else newsearch
    fi
}



newsearch(){
echo -n "$txt2**Search will not proceed**$trst  Perform a new search?  (y/n)"" "
         read newsearch_choice
         let "exit_timer = 0"
         if [ "$newsearch_choice" == "y" ];
         then 
                 echo -n "Clear previous search terms?  (y/n)"" "
                 read clear_decision
                 if [ "$clear_decision" == "y" ];
                 then  let "cycle = 0"
                       unset search_array
                       echo "Previous search terms cleared"
                 fi
                 main          
         else exit 
         fi
}



search() {
find $searchdirectory -type f -print0 | xargs --null grep --color=always -ao "$parsed_short_search\|$parsed_long_search" ./ ; 
#find $searchdirectory -type f -print0 | xargs --null grep --color=always -ao "$first_string\|........$first_string........\|$second_string\|........$second_string........." ./ ; 
#find $searchdirectory -type f -exec grep --color=always -ao "$first_string\|........$first_string........\|$second_string\|........$second_string........" ./ {} \;
}


main


echo "Exit Timer is at: $exit_timer"  "Cycle is: $cycle"
echo "Searching Completed."
Kiah07 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remote Copy Script effektd Bash / Shell Scripting 0 Apr 12th, 2011 8:18 AM
applying the "watch" command to a script yan Bash / Shell Scripting 0 Apr 22nd, 2010 2:19 AM
Can't pass const arguments to function of template class sidlee C++ 7 Feb 16th, 2009 2:49 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC