Visit Our UNIX and Linux User Community


Looking for command(s)/ script to find a text string within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking for command(s)/ script to find a text string within a file
# 1  
Old 11-22-2006
Looking for command(s)/ script to find a text string within a file

I need to search through all files with different file suffixes in a directory structure to locate any files containing a specific string
# 2  
Old 11-22-2006
find . -name '*.*' -exec grep -ilw 'string_name' {} \;

Will this satisfy your requirement ?.
# 3  
Old 11-22-2006
I go for, Smilie

Code:
find . -name '*.*' -print | xargs grep -il 'pattern'

# 4  
Old 11-22-2006
Thanks to both of you

These examples helped. Thank you.

I do not use shell scripting much. Now I can learn from these results.

The results were :

Code:
devr> find . -name '*.*' -exec grep -ilw 'MAXCOMB' {} \;
./IPY0115.prog
./IPY0130.pl
./condir_and_copy_daily.sh
./IPY0130_old.pl
devr> find . -name '*.*' -exec grep -ilw 'maxcomb' {} \;
./IPY0115.prog
./IPY0130.pl
./condir_and_copy_daily.sh
./IPY0130_old.pl
devr> 
devr> | xargs grep -il 'maxcomb' {} \;                      <
grep: can't open {}
grep: can't open ;
grep: can't open {}
grep: can't open ;
./IPY0115.prog
grep: can't open {}
grep: can't open ;
grep: can't open {}
grep: can't open ;
grep: can't open {}
grep: can't open ;
grep: can't open {}
grep: can't open ;
grep: can't open {}
grep: can't open ;
grep: can't open {}
grep: can't open ;
./IPY0130.pl
grep: can't open {}
grep: can't open ;
grep: can't open {}
grep: can't open ;
./test_file_shell.sh
grep: can't open {}
grep: can't open ;
./dir_copy_daily_files.sh
./dir_copy_biweekly_files.sh
./condir_and_copy_biweekly.sh
./condir_and_copy_daily.sh
grep: can't open {}
grep: can't open ;
grep: can't open {}
grep: can't open ;
./doi_dnld_files.prog
./IPY0130_old.pl
grep: can't open ./View
grep: can't open for
grep: can't open value_set
grep: can't open Role_id.sql
devr> rgs grep -il 'maxcomb'                                <
./IPY0115.prog
./IPY0130.pl
./test_file_shell.sh
./dir_copy_daily_files.sh
./dir_copy_biweekly_files.sh
./condir_and_copy_biweekly.sh
./condir_and_copy_daily.sh
./doi_dnld_files.prog
./IPY0130_old.pl
grep: can't open ./View
grep: can't open for
grep: can't open value_set
grep: can't open Role_id.sql
devr> find . -name '*.*' -print | xargs grep -ilw 'maxcomb'
./IPY0115.prog
./IPY0130.pl
./condir_and_copy_daily.sh
./IPY0130_old.pl
grep: can't open ./View
grep: can't open for
grep: can't open value_set
grep: can't open Role_id.sql

# 5  
Old 11-22-2006
you dont have correct permissions to open the file and grep thru it.
# 6  
Old 11-23-2006
Quote:
devr>
devr> | xargs grep -il 'maxcomb' {} \;
xargs and {}
why is that ?

find with xargs grep that would do..

If you dont want to display the output - that files cant be opened
then use 2>/dev/null with find

Previous Thread | Next Thread
Test Your Knowledge in Computers #510
Difficulty: Easy
Variable names cannot be the same as any reserved word as the language would not be able to distinguish between them.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

2. UNIX for Beginners Questions & Answers

Find and replace a string in a text file

Dear all, I want to find all the "," in my text file and then replace the commas to a tab. I found a script online but I don't know how to modify the script for my case. Any one can help? Thank you. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set... (2 Replies)
Discussion started by: forevertl
2 Replies

3. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

4. UNIX for Dummies Questions & Answers

How to pass file text into find command

Hi all, I have a large text file and also a smaller list of program names. I want to find out how many of those programs exist in the large text file. Can someone help me with the command/script please. The program list is along the lines of tranwe2 tranwe3 tranye5 etc so basically I... (5 Replies)
Discussion started by: Grueben
5 Replies

5. Shell Programming and Scripting

How to find repeated string in a text file

I have a text file where I need to find the string = ST*850* This string is repetaed several times in the file, so I need to know how many times it appears in the file, this is the text files: ISA*00* *00* *08*925485USNR *ZZ*IMSALADDERSP... (13 Replies)
Discussion started by: cucosss
13 Replies

6. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

7. Shell Programming and Scripting

find string(s) in text file and nearby data, export to list help

Hi, So I'm kinda new to shell scripts and the like. I've picked up quite a bit of use from browsing the forums here but ran into a new one that I can't seem to find an answer for. I'm looking to parse/find a string AND the next 15 or so charachters that follow the string within a text file... (1 Reply)
Discussion started by: kar23me
1 Replies

8. Shell Programming and Scripting

Find string in text file

Hello! Please, help me to write such script. I have some text file with name filename.txt I must check if this file contains string "test-string-first", I must cut from this file string which follows string "keyword-string:" and till first white-space and save it to some variable. For... (3 Replies)
Discussion started by: optik77
3 Replies

9. Shell Programming and Scripting

How to redirect a input of find command into a text file

Hello friends, I want a command to print the reult files from find command into a text file.:) Iam looking from forum memebers. PLZ help me.ASAP Thanks in Advance, Siva Ranganath CH (5 Replies)
Discussion started by: sivaranga001
5 Replies

10. Shell Programming and Scripting

find a string in a file and add some text after that file

Hi Could you please help me out by solving teh below problem ? I have a file with as below source1|target1|yes source2|target2|no source1 is file in which i have to place some code under the <head> tag in it. What code i have to place in source1 is something like this "abcd.....<target1>... (5 Replies)
Discussion started by: Tasha_T
5 Replies

Featured Tech Videos