find PHRASE and PATH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find PHRASE and PATH
# 1  
Old 01-07-2009
find PHRASE and PATH

I've got a script which finds *.txt files in directories and subdirectories after providing the path by the user and then searches in the files for phrase given by the user

Quote:
#!/bin/bash
PATH_BIN=/bin
PATH_USR=/usr/bin
DIRECTORY=$1
PHRASE=$2
echo "";
read -p "Provide path to directory to find *.txt: " DIRECTORY
read -p "State what phrase you're looking for in the files *.txt: " PHRASE

function PHRASE_ {
if [ $PHRASE ];
then
echo "";
FOLDERS_=`$PATH_USR/find $DIRECTORY -name *.txt -print`
WRITE_=`$PATH_USR/find $DIRECTORY -name *.txt -print | $PATH_USR/xargs $PATH_USR/catdoc -a | $PATH_BIN/grep -n "$PHRASE"`
echo "$WRITE_" >> ${PATH}/temp/write_values.txt
exit -1;
# echo "The PHRASE is in : " ${FOLDERS_};
else echo "The PHRASE does not exist *.txt"
fi;
}
PHRASE_
How to write script in such way that the paths to the found *.txt files and the phrase given by the user were both displayed ?

For example:

Quote:
$./script.sh
Provide path to directory to find *.txt: /home/przemek/tempowy/find_txt/Catalogue
State what phrase you're looking for in the files *.txt: <value>
/home/przemek/tempowy/find_txt/Catalogue/data/common/file.txt
<value>
/home/przemek/tempowy/find_txt/Catalogue/data/common/tools/file.txt
<value>
...
# 2  
Old 01-07-2009
I usually hack my way through this as follows:
Code:
find $DIRECTORY -name *.txt -print | 
while read file; do
   echo $file
   $PATH_USR/catdoc -a $file | grep -n "$PHRASE"
done

But that leaves you with files being printed out that don't contain PHRASE. So you could do this, which requires a catdoc and grep for each file:
Code:
find $DIRECTORY -name *.txt -print | 
while read file; do
   if catdoc -a $file | grep "$PHRASE" >/var/tmp/phrase-grep-$$ ; then 
     echo $file
     cat /var/tmp/phrase-grep-$$
     rm -f /var/tmp/phrase-grep-$$
   done
done

# 3  
Old 01-07-2009
Thank you very much Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

How to find the path of a command?

Hi guys. I want to know the path of a command. I tried "which" command also . But no luck. Please tell me how to find and update the correct path of the command. Here I'm unable to find the path of ext2online command # resize2fs /dev/vg01/lvora_backup resize2fs 1.39 (29-May-2006)... (3 Replies)
Discussion started by: vamshigvk475
3 Replies

2. Shell Programming and Scripting

How to find a phrase and pull all lines that follow until the phrase occurs again?

I want to burst a report by using the page number value in the report header. Each section starts with *PAGE NO:* 1 Each section might have several pages, but the next section always starts back at 1. So I want to find the "*PAGE NO:* 1" value and pull all lines that follow until "*PAGE NO:* 1"... (4 Replies)
Discussion started by: Scottie1954
4 Replies

3. Shell Programming and Scripting

Find the length of a path

Hey, I got a question: how do I find the length of a path ? this is the context: I used this funtion find: DIR="/home/name/directory" find DIR -name "*.extension" The output is then: /home/name/directory/filename.extension What I'd like to have is only the last part:... (3 Replies)
Discussion started by: Miki1579
3 Replies

4. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

5. Shell Programming and Scripting

How to a find a file in which path it is there?

Hi Firends, Good Morning to all, I want a find command to search a paticular file present in my system(ie search through under all users and all directories.) I am looking forward from you.:) Advance Thanks, Siva Ranganath CH (3 Replies)
Discussion started by: sivaranga001
3 Replies

6. Shell Programming and Scripting

How to find the path

Hi Iam new to this forum, could you pleae advise for below question iam send path as input before proceding i need to validate the path if it is exit or not if not it show path id not exist if the path is exits iam sending file name as input to search the file on the paricular... (5 Replies)
Discussion started by: saic
5 Replies

7. Shell Programming and Scripting

Shell script to find longest phrase

Hi Everyone, I am trying to write a shell script that can find the longest phrase that appears at least twice in an online news article. The HTML has been parsed through an HTML parser, converted to XML and the article content extracted. I have put this article content in a text file to work... (24 Replies)
Discussion started by: stargazerr
24 Replies

8. Shell Programming and Scripting

how to find the path of a file?

Hi all, Is there any way to find the the path of a file? I mean executable files and just anyother file we can think of? i know of one cmd called which $which mount /usr/bin/mount this is fine, but "mount" is a cmd not a file that can be searched eg: say i have created a text file... (3 Replies)
Discussion started by: wrapster
3 Replies

9. Shell Programming and Scripting

negation in find path

Guys, Pl suggest me how to ignore a path in find command... I am aware of using "!" for other option... but how can i use it like this exampl... I want to search something for in / but not in Pl help. Thanks.. (2 Replies)
Discussion started by: clx
2 Replies

10. Solaris

Please help me find the PATH that I need to be on :)

I am learning UNIX through the School of Hard Knocks, I have very limited exposure to it. I managed to install Solaris 10 on my machine recently, and have downloaded and installed Sun Studio 11. When the installation of Sun Studio was complete, the GUI displayed the following: (Bourne shells)... (7 Replies)
Discussion started by: trmn8r
7 Replies
Login or Register to Ask a Question