Using find to output list of files with specific strings

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using find to output list of files with specific strings
# 1  
Old 02-20-2017
Using find to output list of files with specific strings

This is my problem, I am using the following code to extract the file names with specific strings 0.01:
Code:
find ./ -name "*.txt" -exec grep -H '0.01' {} +

It works wonders with a small sample. However, when I use it in a real scenario it produces an empty file -even though I am sure there are files with the expected string
Am I missing something here? I am pretty sure there should be a better alternative using AWK, I just could not come up with one
Thanks in advance
# 2  
Old 02-20-2017
I couldn't find any example of -H in reference books and grep warned me of "illegal option" when I tried it. Perhaps just "-l" instead. Also, the "find" commands that execute something end in "\;" instead of a plus sign. Putting a backslash in front of your decimal point might be worth a look.
# 3  
Old 02-20-2017
It were highly surprising if grep should fail in "real scenarios". Does "I am sure there are files" guarantee to 100% there are files? Why don't you add a test file to the real scenario to check for correct operation?
@wbport: There are grep versions (including linux and FreeBSD) providing the -H option to print file names. And, most (if not all) find commands allow the exec action to be terminated with a ; (for execing on every single file found) OR with a + (for execing on as many files as would fit). The unescaped dot in the regex will match any char including decimal points, so the missing matches will NOT be due to this.
# 4  
Old 02-20-2017
Quote:
Why don't you add a test file to the real scenario to check for correct operation?
I did. That's why I know there is something wrong with the performance of the script. In reality, I wanted list all files where values between 0.019-0.011 were found -I just could not come up with a better solution.
As I said, it seem to work in a subset of files but failed miserably using real datasets SmilieSmilieSmilie
# 5  
Old 02-20-2017
Post a representative sample of an input file from the real dataset that should match but doesn't.
# 6  
Old 02-20-2017
Quote:
Originally Posted by Xterra
This is my problem, I am using the following code to extract the file names with specific strings 0.01:
Code:
find ./ -name "*.txt" -exec grep -H '0.01' {} +

It works wonders with a small sample. However, when I use it in a real scenario it produces an empty file -even though I am sure there are files with the expected string
Am I missing something here? I am pretty sure there should be a better alternative using AWK, I just could not come up with one
Thanks in advance
Would this work?
Code:
find . -name '*.txt' -type f -print0 | xargs -0 perl -nle '/0.01/ and print $ARGV and close ARGV'

# 7  
Old 02-21-2017
If you just want the names of files whose names end in .txt and whose contents include the string 0.01 (without printing the contents of lines that contain that string), I would try:
Code:
find . -name '*.txt' -exec grep -Fl 0.01 {} +

PS: Note that since I'm using grep -F (AKA fgrep) instead of grep without the -F option, we are looking for a fixed string instead of looking for a match to a basic regular expression. Therefore, we don't need to escape the <period> in the string 0.01 to keep it from matching any character as it would in a BRE match.

Last edited by Don Cragun; 02-21-2017 at 12:16 AM.. Reason: Add PS.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. Shell Programming and Scripting

Exclude particular files from strings output

Hi everyone, Is it possible to have the command strings exclude particular files? Here is what I am currently writing: strings *20161212* It prints all files in the directory, which is good, but some file types do not need to be printed because they contain gibberish. I am trying the... (5 Replies)
Discussion started by: clippertm
5 Replies

3. Shell Programming and Scripting

Perl script to find process and exclude strings from the output

Hi team, I'm a newbie of Perl Script and looking to create a simple perl script that will run in the Linux system: 1) to find process, such as ps -ef | grep process name 2) to exclude strings from the output if it found, for instance if i see abc from usr process, then will exclude it from... (1 Reply)
Discussion started by: hoffman2503
1 Replies

4. Shell Programming and Scripting

Getting specific strings from output

so i have the following string: ... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents. -----cat 1.txt----- 1234 5678 1256 1234 1247 ------------------- I have 3 more files in a folder -----ls -lrt------- A1.txt A2.txt A3.txt ------------------- The contents of those three files are similar format with different data values... (8 Replies)
Discussion started by: realspirituals
8 Replies

6. UNIX for Dummies Questions & Answers

Find Multiple Strings from a list of *.gz files withour decompressing...

Hello Team, There is this situation where there are around 20 *.gz files and i want to search multiple words from all those files. Example as below : filea.gz fileb.gz filec.gz now i want to search words "hi" and "hello" from all these 3 files without... (4 Replies)
Discussion started by: varun87
4 Replies

7. Shell Programming and Scripting

output strings to specific positions in a file

Been searching for about 3 hours for similar functionality that I can get examples of how to output text from variables into certain locations in a file. I would like to incorporate this into a script. I have not been able to find a command example that does it all in one method. I find part of... (1 Reply)
Discussion started by: bennu_500
1 Replies

8. Shell Programming and Scripting

Script to find NOT common strings in two files

Hi all, I'd like you to help or give any advise about the following: I have two (2) files, file1 and file2, both files have information common to each other. The contents of file1 is a subset of the contents of file2: file1: errormsgadmin esdp esgservices esignipa iprice ipvpn irm... (0 Replies)
Discussion started by: hnux
0 Replies

9. Shell Programming and Scripting

I need a script to find socials in files and output a list of those files

I am trying to find socail security numbers in files in (and under) a specific directory and output a list of the files where they are found... the format would be with no dashes just 9 numeric characters in a row. I have tried this: find /DirToLookIn -exec grep '\{9\}' /dev/null {} \; >>... (1 Reply)
Discussion started by: NewSolarisAdmin
1 Replies

10. Shell Programming and Scripting

Find Strings in Files

experts, i am using solaris 9. there are serveral *.log files in a directory "/var/alllogs/". Among the files one or several files contain the string "0198634873". I want do- cat *.log | grep "0198634873"; And want to see which Files(file_name) contain that string. Plus output will go to a... (3 Replies)
Discussion started by: thepurple
3 Replies
Login or Register to Ask a Question