Best Alternative to Search Text strings in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Best Alternative to Search Text strings in directory
# 8  
Old 05-09-2011
Java

Awk is most likely not your bottleneck. It can loop through the list super fast and print out the commands without much delay. However, each line of command is a grep for a particular string (name) on ALL files inside there. Now that must take some time.
I think after those few hundred you're seeing delays from the searching...
I'd guess it could be sped up by instructing grep to search for multiple strings at a time, rather than only one name; especially since the files that search is performed on, are the same for all strings.
And I did have the feeling this is an overkill. Much simpler and more effective here:
Code:
grep -io -f  Customers.lst *

-f option tells grep that patterns to search for are specified in file Customers.lst
-o option prints just the part of line that matches pattern (just the name)

This should work much better. No need to spawn a myriad of grep processes.

This may, however produce duplicate lines; in case there is multiple lines that contain the same name. So you might want to pipe it to uniq(1) to get rid of duplicates.
Its output is in form:
Code:
file.log:NameMatched

so you want to flip this around to get what you want:
Code:
sh$ echo file.log:NameMatched | sed 's/\([^:]*\):\([^:]*\)/\2, \1/'
NameMatched, file.log

So to put it all together:
Code:
grep -io -f  Customers.lst *  | uniq | sed 's/\([^:]*\):\([^:]*\)/\2, \1/'

I tried the -F swithc of grep which seems to run much faster, and produce the same output, but I'm not sure what it actually does Smilie Man page says:
Quote:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.
Since our patterns are in an external file, and separated by newlines, this should not have much effect. But it does speed up things... If anyone can bring some light into -F switch, please enlighten me...
# 9  
Old 05-09-2011
Hi ,

THanks for the reply. I will try these today and will send you an update.

Arun
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a text between two strings in a file using regex

Here is my sample file data: My requirement is to have a regex expression that is able to search for visible starting string "SSLInsecureRenegotiation Off" between strings "<VirtualHost " and "</VirtualHost>". In the sample data two lines should be matched. Below is what I tried but... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

Search between two search strings and print the value

Based on the forums i have tried with grep command but i am unable to get the required output. search this value /*------ If that is found then search for temp_vul and print and also search until /*------- and print new_vul Input file contains: ... (5 Replies)
Discussion started by: onesuri
5 Replies

5. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

6. Shell Programming and Scripting

Search replace strings between single quotes in a text file

Hi There... I need to serach and replace a strings in a text file. My file has; books.amazon='Let me read' and the output needed is books.amazon=NONFOUND pls if anybody know this can be done in script sed or awk.. i have a list of different strings to be repced by NONFOUND.... (7 Replies)
Discussion started by: Hiano
7 Replies

7. UNIX for Dummies Questions & Answers

How to insert alternative columns and sort text from first column to second?

Hi Everybody, I am just new to UNIX as well as to this forum. I have a text file with 10,000 coloumns and each coloumn contains values separated by space. I want to separate them into new coloumns..the file is something like this as ad af 1 A as ad af 1 D ... ... 1 and A are in one... (7 Replies)
Discussion started by: Unilearn
7 Replies

8. UNIX for Dummies Questions & Answers

Search for a text within files in a directory

I need to search for a particular string. This string might be present in many files. The directory in which I am present has more than one subdirectories. Hence, the search should check in all the subdirectories and all the corresponding files and give a list of files which have the particular... (5 Replies)
Discussion started by: pakspan
5 Replies

9. Shell Programming and Scripting

how to do search and replace on text files in directory

I was google searching and found Perl as a command line utility tool This almost solves my problem: find . | xargs perl -p -i.old -e 's/oldstring/newstring/g' I think this would create a new file for every file in my directory tree. Most of my files will not contain oldstring and I... (1 Reply)
Discussion started by: siegfried
1 Replies

10. Shell Programming and Scripting

Search between strings with an OR

Hi have Input in this way KEY AAAA BBBB END1 KEY AAAA BBBB END2 KEY AAAA BBBB END3 I need to find any thing matching in between KEY And ending with "END1|END2|END3" This didnot work awk '/KEY/,/END1|END2|END3/' (3 Replies)
Discussion started by: pbsrinivas
3 Replies
Login or Register to Ask a Question