Finding files which contains anyone from the given patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding files which contains anyone from the given patterns
# 1  
Old 11-05-2012
Finding files which contains anyone from the given patterns

Hi All,
I need help as i am not able to create shell script for a scenario.

i have 3000 numbers and want to search all the files which contain anyone of the above pattern.

the files are in folder structure.

Thanks and Regards
Rishi Dhawan
# 2  
Old 11-05-2012
Quote:
Originally Posted by Rishi26
i have 3000 numbers and want to search all the files which contain anyone of the above pattern.
This is very confusing explanation.
Please try to give some inputs, sample files so it could be easy to understand.Smilie

Assuming
1) Your pattern are in file.
2) You want filename as a result.

try
Code:
for file in /file_path/*
do
if [[ $(grep -f search_patterns $file) ]]
then
echo $file
fi
done

# 3  
Old 11-05-2012
i have a file aa.txt which contains numbers to be searched
E.G.
Code:
"123"
"456"
"789"

like this i have 3000 patterns in that file.

i have to search if any of the above patterns is available in the files in all the sub folders

Last edited by Scott; 11-06-2012 at 02:34 AM.. Reason: Code tags
# 4  
Old 11-05-2012
Quote:
Originally Posted by Rishi26
i have to search if any of the above patterns is available in the files in all the sub folders
If you want to print the result, then

Code:
# search_patterns is file which contains your all patterns.
# /file_path/ - Give your directory path from which you want to search.
for file in /file_path/*
do
grep -f search_patterns $file
done

This User Gave Thanks to pamu For This Post:
# 5  
Old 11-05-2012
If available - you don't mention your system details - grep's -r option will descend into directories by itself.
# 6  
Old 11-05-2012
Code:
find . -type f -exec grep -f aa.txt {} \;

# 7  
Old 11-06-2012
i am not able to execute above script in my unix box.
i am giving command

Code:
find . -type f - exec fgrep aa.txt {} \;

if this is not exact can you give me exact command.

and same output i am receiving with
Code:
for file in /file_path/*
do
  grep -f search_patterns $file
done

Thanks

Last edited by Scott; 11-06-2012 at 02:34 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Finding matching patterns in two files

Hi, I have requirement to find the matching patterns of two files in Unix. One file is the log file and the other is the error list file. If any pattern in the log file matches the list of errors in the error list file, then I would need to find the counts of the match. For example, ... (5 Replies)
Discussion started by: Bobby_2000
5 Replies

3. Shell Programming and Scripting

How to group matched patterns in different files

Hi, I have a master file that i need to split into multiple files based on matched patterns. sample of my data as follows:- scaff_1 a e 123 130 c_scaff_100 scaff_1 a e 132 138 c_scaff_101 scaff_1 a e 140 150 ... (2 Replies)
Discussion started by: redse171
2 Replies

4. Shell Programming and Scripting

Finding several patterns and outputting 4 lines after

I an trying to parse a file looking for pattern1, or pattern2, or pattern3 and when found print that line and the next 4 lines after it. I am using korn shell script on AIX and grep -A isn't available. (1 Reply)
Discussion started by: daveisme
1 Replies

5. Shell Programming and Scripting

Finding patterns in a file

Hi, I have a file with 3 columns and I want to find when the average number of rows on column 3 is a certain value. The output will be put into another file indicating the range. Here is what I mean (file is tab separated): hhm1 2 0 hhm1 4 0.5 hhm1 6 0.3 hhm1 8 -1.4... (2 Replies)
Discussion started by: kylle345
2 Replies

6. Shell Programming and Scripting

Need help with awk tool - finding text between two patterns

This is regarding using awk tool to find lines matching between 2 patterns. cat file | awk '/pat1/,/pat2/' But it's not working as expected in the following case. If pat1 also comes after pat2 then it's matching whole file after pat1. e.g. # > cat -n file 1 First line... (3 Replies)
Discussion started by: anand_bh
3 Replies

7. Shell Programming and Scripting

finding and removing patterns in a large list of urls

I have a list of urls for example: Google Google Base Yahoo! Yahoo! Yahoo! Video - It's On Google The problem is that Google and Google are duplicates as are Yahoo! and Yahoo!. I'm needing to find these conical www duplicates and append the text "DUP#" in from of both Google and... (3 Replies)
Discussion started by: totus
3 Replies

8. UNIX Desktop Questions & Answers

how to search files efficiently using patterns

hi friens, :) if i need to find files with extension .c++,.C++,.cpp,.Cpp,.CPp,.cPP,.CpP,.cpP,.c,.C wat is the pattern for finding them :confused: (2 Replies)
Discussion started by: arunsubbhian
2 Replies

9. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

10. Shell Programming and Scripting

Finding patterns through out all subdir

Hi all experts, here is a problem which i would appreciate ur expertise. I need to do this: Eg. Find a number: 1234567 which i dunno which file and which folder I do know which main folder it is in but it is hidden deep within a lot of subdir. Is it possible to find the file? + output... (4 Replies)
Discussion started by: unnerdy
4 Replies
Login or Register to Ask a Question