Find word in a directory files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find word in a directory files
# 1  
Old 02-13-2013
Find word in a directory files

Hi folks,

i have a file in which i have words like below

words.txt(file name) and words are arranged likes no spaces between words
Apple
Banana
Carrot

And i have list files which have these words in a directory (/tmp/satemp)
test1.txt
test2.txt
test3.txt

So if the word 'Apple' is exist in test1.txt and test2.txt then i should get the output as
'Apple' found in test1.txt
'Apple' found in test2.txt

Hope you understand my requirement.

Please help me on this

Many thanks
# 2  
Old 02-13-2013
Is this homework?
What have you tried?
# 3  
Old 02-13-2013
This is what i have tried, please correct my script

where object_list.lst have words list and /tmp/satemp (is the dir where all files are there)
Code:
#!/bin/ksh
FILE=/tmp/satemp
object_list_arr=(`cut -f 1 /tmp/satemp/object_list.lst`)

for i in ${object_list_arr[@]}
do
         grep -w "$i" object_list_arr >/tmp/satemp/temp.log

if [ $? -eq 0 ]
then
   echo "Word found!" "$i"
else
   echo "Word NOT found!" "$i"
fi
done

# 4  
Old 02-13-2013
Code:
for f in /tmp/satemp/*
do
cat words.txt| while read line
do 
awk -vLIT="$line" '$0 ~ LIT { print LIT,"Found in ",FILENAME; }' $f
done
done


Last edited by busyboy; 02-13-2013 at 02:52 AM.. Reason: code tags
This User Gave Thanks to busyboy For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

2. Shell Programming and Scripting

Find command to find a word from list of files

I need to find a word '% Retail by State' in the folder /usr/sas/reports/RetailSalesTaxallocation. When I tried like below, -bash-4.1$ cd /usr/sas/reports/RetailSalesTaxallocation -bash-4.1$ find ./ -name % Retail by State find: paths must precede expression: Retail Usage: find ... (10 Replies)
Discussion started by: Ram Kumar_BE
10 Replies

3. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

4. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

5. Shell Programming and Scripting

How can i find a word inside a file from a directory and it's subdirectory ?

Suppose i have a word "mail". I have to search this word in all files inside a directory and it's sub-directories. It will also search in all hidden directory and sub-directories. If it finds this word in any file it will list that file. How can i do this with perl/ruby/awk/sed/bash or... (9 Replies)
Discussion started by: cola
9 Replies

6. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

7. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

8. UNIX for Dummies Questions & Answers

How to find a word in a all the files in a Directory??

I want to find a specific word present in all the files ina directory....Please tell me the command to be used?? Thanks (6 Replies)
Discussion started by: shikhakaul
6 Replies

9. Shell Programming and Scripting

Shell:Find a word in files in a directory and subdirectories

I'm looking to write a ksh code with will be alble to find a word like 'toto' in all files going from my current directory. eg. /doc ----------->have: text.c which "toto" /doc/usr-------->have: build.pc, help.java which "toto" /doc/usr/cach -->have: test.sh which "toto" /doc/build... (4 Replies)
Discussion started by: yeclota
4 Replies

10. UNIX for Dummies Questions & Answers

search for certain word in a files from directory

hi can i know how to get the file name of any files containing for example "abc" from a number of files in a directory? i tried "ls -ltrc | grep abc" but no resulted generated. thanks in advance. (1 Reply)
Discussion started by: legato
1 Replies
Login or Register to Ask a Question