Looking for a string in files and reporting matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking for a string in files and reporting matches
# 1  
Old 03-28-2007
Looking for a string in files and reporting matches

Can someone please help me figure out what the command syntax I need to use is?

Here is what I am wanting to do.

I have hundreds of thousands of files I need to look for a specific search string in.
These files are spread across multiple subdirectories from one main directory.
I would like to find a way to search through all these files, and everytime it sees a file with this specific search string in it, make a note of it in a log file.

So, for instance, I have the directory /files/accounting/
Under /files/accounting, I have LOTS of Microsoft Excel files that I want to search for the specific string "\\cttrut04\dept\" and then everytime it finds a file by that name, dump it to a log file.

I thought about something like:

find /files/accounting/ -name "*xls" | more | grep -i "\\clttrut04\dept" > \log\filelog.txt

This doesnt work. I am not sure how to do a find and then feed each file name it finds to more to search it and then grep it for the search string.

Can anyone help with this?

Thank you,
Brian
# 2  
Old 03-28-2007
find /files/accounting/ -name *.xls -exec grep -i '\\\\clttrut04\\dept' /dev/null {} \;
# 3  
Old 03-28-2007
Maybe change grep -i to grep -i -l to report the filename. This is what I take "make a note of it" to mean.
# 4  
Old 03-28-2007
Quote:
Originally Posted by gopidesaboyina
find /files/accounting/ -name *.xls -exec grep -i '\\\\clttrut04\\dept' /dev/null {} \;
Where does the output of this go?

Also, why do you have four \\\\'s and two \\'s, when the search string is only \\ and \?


Thanks for your help!!!
# 5  
Old 03-29-2007
output shows on screen. if you want you can redirect them to some file. those extra slashes are for escape, other wise it wouldn't find that string as / has special meaning in unix.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Display text is string matches below.

I have not idea how I would accomplish this. I have a script that scans for CDP neighbours. I get the results in a file. I am interested in CDP Neighbor Details if name BSWITCH shows up. If BSWITCH is not present then skip. CDP Neighbor Details for 10.200.21.1... (1 Reply)
Discussion started by: mrlayance
1 Replies

3. UNIX for Dummies Questions & Answers

Reporting characters after string

I have a file that looks like this: >ID 1 AATAATTCCGGATCGTGC >ID 2 TTTGACAGTAGAC >ID 3 AGACGATGACGAT I am using the following script to report if AATTCCGGATCG is present in any sequence: awk 'FNR==1{n=substr(FILENAME,1,index(FILENAME,".")-1)} { print n "\t"... (10 Replies)
Discussion started by: Xterra
10 Replies

4. UNIX for Dummies Questions & Answers

Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90. while read -r line do grep -q "$line" File2.txt && echo "$line +" || echo "$line -" done < File1.txt ' Input file 1: ... (5 Replies)
Discussion started by: bernardo.bello
5 Replies

5. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

6. Shell Programming and Scripting

If string matches within 2 files, delete one file.

I have a directory with a large # of files and in each file I am looking to match a string in one file with a string in the subsequent n file(s). If there is a match between a string in one file and a string in the next n file(s) then delete the subsequent duplicate file(s). Here is sample input: ... (2 Replies)
Discussion started by: sitney
2 Replies

7. Shell Programming and Scripting

How to remove all matches in a string with sed

if I have "abxcdxefx" and want to remove the x's with sed, how can I do this? Thanks. WHOOPS: Just remembered: echo "abxcdxefx" | sed s/x//g Thanks for reading, though. (0 Replies)
Discussion started by: lumix
0 Replies

8. Shell Programming and Scripting

to create foder when the string matches

hi :) im actually new to shell script ,see here is the program wat it will do is .. i have to create folder at the run time when the string matches ... what to do is im havin text file which carry the file name like ( EngCVer1pg1j01.TOP, EngCVer1pg1m08.TOP, EngCVer1pg1h04,..and... (1 Reply)
Discussion started by: maximas
1 Replies

9. Shell Programming and Scripting

String in text matches folder name

Hi, I need unix shell script that can read the first column of a text file and matching each column string is a folder and i need to read files from the specified folder e.g Main.txt has Mike 690 Jhon 346 i need to read Mike first then open up the files in folder Mike in the same... (2 Replies)
Discussion started by: shackman66
2 Replies
Login or Register to Ask a Question