Goto each directory and subdirectories and grep the required pattern


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Goto each directory and subdirectories and grep the required pattern

Hi All,

I need your help in finding pattern from files present in different directories.

I need to search for a pattern "xyz" from "*.txt" files which are present in different levels of directories as shown.

example
-------

dir1/subdir1/file.txt
dir2/subdir2/subsubdir2/file.txt
dir3/subdir3/file.txt
dir4/subdir4/subsubdir4/subsubsubdir4/file.txt
..
..
..
dirn/...


If the pattern xyz is found in dir2... and dir3.. then i want the result saved as shown.

result.txt
---------
dir2/subdir2/subsubdir2
dir3/subdir3


The only static value is extension of file name (.txt) apart from this the directory names and file names can change in future.

Appreciate your help in Advance.

Thanks
imas.
# 2  
something like..

Code:
find <path to where all the dir and sub dir exists> -name "*.txt" -type f 2>/dev/null | xargs grep -l 'xyz'

this will print the filenames if it matches the pattern.
you can get the dirname further and then unique values.
# 3  
try this, not the exact output but closer to it

Code:
 
find /dir/to/search -name "*.txt" | xargs grep -l xyz

# 4  
Thank you for the quick reply.

I will update you about the results tomorrow once i get back to my terminal.

-Regards,
imas

---------- Post updated 02-03-10 at 10:42 AM ---------- Previous update was 02-02-10 at 08:40 PM ----------

Hi,

This will give me the location of all the files.

find /dir/to/search -name "*.txt" | xargs grep -l xyz

But how do I write a script that will go into each directory and again search for another pattern from the *.txt file.

example
--------
if xyz pattern is found in the following directories.

dir2/subdir2/subsubdir2/file1.txt
dir3/subdir3/file2.txt
dir4/subdir4/subsubdir4/file3.txt


Please do not get confuse my actual requirement is to find the files in different directories which contains pattern xyz

Then go to each of the directories where you found xyz pattern in that file and again search for 2nd pattern from this file.

Appreciate your help.

Thanks
imas
# 5  
Quote:
Then go to each of the directories where you found xyz pattern in that file and again search for 2nd pattern from this file.

you want to grep the second pattern in the "first pattern found files" or in all the files from the directories where at least one file having the first pattern exists. ?
# 6  
MySQL

Hi Khare,

Yes you are correct.

After the finding my 1st pattern I just need to cat each of the result using for loop and grep for my required 2nd pattern and get the required result.

Thank you very much.

PS: Neo/moderator please close this thread.

-imas.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #67
Difficulty: Medium
IT security or cybersecurity controls are based on a combination of three types of security controls, (1) physical controls, (2) logical controls and (3) administrative controls.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

2. Shell Programming and Scripting

Sed/awk : to grep only required pattern disk

Hi Experts, Need help with the following: Desired output: Only want to get the output marked in green. The file: --- Physical volumes --- PV Name /dev/disk/disk4704 PV Status available Total PE 6399 Free PE ... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

Script to process file from a directory and grep the required content and print

Hi All, below script reads the perticular files from the directory. Am trying to fetch status and print them in the required format. It needs to read line and search for string "Passed/Failed" and print them under correct sub header. script : BASE_DIR=/tmp/test/REPORT/CollectReport #... (16 Replies)
Discussion started by: Optimus81
16 Replies

4. Shell Programming and Scripting

Use awk/sed/grep with goto statement!

Hi, I have an array with characters and I am looking for specific character in that array and if those specific character not found than I use goto statment which is define somehwhere in the script. My code is: set a = (A B C D E F) @ i = 0 while ($i <= ${#a}) if ($a != "F" || $a != "D")... (3 Replies)
Discussion started by: dixits
3 Replies

5. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

6. Shell Programming and Scripting

Copying subdirectories of a directory to some other directory and renaming them

Hi, I am a newbie in shell scripting. I have to copy a particular sub-directory (data) from a large no. of directories (all in the same folder) and paste them to another directory ( /home/hubble/data ) and then rename all the subdirectories (data) as the name of its parent directory. please... (8 Replies)
Discussion started by: sholay
8 Replies

7. Solaris

Goto last visted directory

Dear All, Can any one pls let me the command for how to goback to previous visited directory from the current working directory in SunOS ? In case of HP-UX; the same can be resolved through "cd -" command. Thanks in advance! Prasanth Babu. (6 Replies)
Discussion started by: prasanth_babu
6 Replies

8. UNIX for Dummies Questions & Answers

grep required pattern and next 2 or 3 lines

dear ones pl.kindly help me 1) how to print(grep) required pattern and following 2 or 3 lines. 2) grep required pattern(to print)+above 2 lines+below 2 or 3 lines.from a report file. ex: we have some report file kf askfjsk fksaj fk skf sjfksjd kff sjfkjs kf jskdjfklsd jfklsdf sdkfjsd fsd... (3 Replies)
Discussion started by: cvvsnm
3 Replies

9. Shell Programming and Scripting

How Can I Make Subdirectories In A Directory?

I cant remember how, i use to know but its been like 2 years since ive used shell can anyone help me? (7 Replies)
Discussion started by: kprescod4158
7 Replies

10. UNIX for Dummies Questions & Answers

makefiles in a directory and subdirectories

I need to develop a makefile that spans across directories. For example, let's say i have an upper level directory (main) and about 2 subdirectories. I want my .cpp files and .o files to be in one subdirectory. I want my .a files to be in the other subdirectory. The .a files are made up of the... (0 Replies)
Discussion started by: benjie_asu
0 Replies

Featured Tech Videos