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
# 8  
Old 02-21-2017
My script
find ./ -name "*.9tmp" -exec grep -H '0.1' {} +

outputs (subset):
Code:
 ./9.9tmp:0.609114
 ./9.9tmp:0.609114
 ./9.9tmp:0.609114
 ./91.9tmp:0.570516
 ./91.9tmp:0.570516
 ./92.9tmp:0.409131
 ./93.9tmp:0.904146
 ./93.9tmp:0.904146
 ./94.9tmp:0.609114
 ./97.9tmp:0.570516

My modified script
find ./ -name "*.9tmp" -exec grep -H '0.[0-1][0-9][0-9][0-9][0-9][0-9]' {} +

outputs:
Code:
 ./11.9tmp:0.179054
 ./11.9tmp:0.152542
 ./11.9tmp:0.152542
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.152542
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465

Aia's script find . -name '*.txt' -type f -print0 | xargs -0 perl -nle '/0.01/ and print $ARGV and close ARGV'
outputs:
Code:
 ./11.9tmp
 ./12.9tmp
 ./16.9tmp
 ./20.9tmp
 ./22.9tmp
 ./26.9tmp
 ./28.9tmp
 ./29.9tmp
 ./30.9tmp
 ./32.9tmp
 ./36.9tmp
 ./42.9tmp
 ./46.9tmp
 ./48.9tmp
 ./53.9tmp
 ./57.9tmp
 ./61.9tmp
 ./62.9tmp
 ./63.9tmp
 ./69.9tmp
 ./72.9tmp
 ./75.9tmp
 ./83.9tmp
 ./9.9tmp
 ./91.9tmp
 ./92.9tmp
 ./93.9tmp
 ./94.9tmp
 ./97.9tmp

And Don's script
find . -name '*.9tmp' -exec grep -Fl 0.1 {} +

Outputs:
Code:
 /11.9tmp

So, my modified script and Don's output the expected/desired result. Why is that my first script and Aia's do not produce the same result?

SmilieSmilie

Last edited by Xterra; 02-21-2017 at 05:10 PM..
# 9  
Old 02-21-2017
Quote:
Originally Posted by Xterra
[...]Why is that my first script and Aia's do not produce the same result?

SmilieSmilie
In post #1 you said:
Quote:
This is my problem, I am using the following code to extract the file names with specific strings 0.01:
I interpreted that you wanted to output just the file name.
If you want as well to show the first instance of the matching pattern, try the following:
Code:
find . -name '*.txt' -type f -print0 | xargs -0 perl -nle '/0\.01/ and print "$ARGV: $&" and close ARGV'

This User Gave Thanks to Aia For This Post:
# 10  
Old 02-21-2017
Got it!
Thanks!
# 11  
Old 02-21-2017
Quote:
Originally Posted by Xterra
My script
find ./ -name "*.9tmp" -exec grep -H '0.1' {} +

outputs (subset):
Code:
 ./9.9tmp:0.609114
 ./9.9tmp:0.609114
 ./9.9tmp:0.609114
 ./91.9tmp:0.570516
 ./91.9tmp:0.570516
 ./92.9tmp:0.409131
 ./93.9tmp:0.904146
 ./93.9tmp:0.904146
 ./94.9tmp:0.609114
 ./97.9tmp:0.570516

My modified script
find ./ -name "*.9tmp" -exec grep -H '0.[0-1][0-9][0-9][0-9][0-9][0-9]' {} +

outputs:
Code:
 ./11.9tmp:0.179054
 ./11.9tmp:0.152542
 ./11.9tmp:0.152542
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.152542
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465

Aia's script find . -name '*.txt' -type f -print0 | xargs -0 perl -nle '/0.01/ and print $ARGV and close ARGV'
outputs:
Code:
 ./11.9tmp
 ./12.9tmp
 ./16.9tmp
 ./20.9tmp
 ./22.9tmp
 ./26.9tmp
 ./28.9tmp
 ./29.9tmp
 ./30.9tmp
 ./32.9tmp
 ./36.9tmp
 ./42.9tmp
 ./46.9tmp
 ./48.9tmp
 ./53.9tmp
 ./57.9tmp
 ./61.9tmp
 ./62.9tmp
 ./63.9tmp
 ./69.9tmp
 ./72.9tmp
 ./75.9tmp
 ./83.9tmp
 ./9.9tmp
 ./91.9tmp
 ./92.9tmp
 ./93.9tmp
 ./94.9tmp
 ./97.9tmp

And Don's script
find . -name '*.9tmp' -exec grep -Fl 0.1 {} +

Outputs:
Code:
 /11.9tmp

So, my modified script and Don's output the expected/desired result. Why is that my first script and Aia's do not produce the same result?


SmilieSmilie
Expanding a little on what I said in post #7, grep 0.1 and grep -E 0.1 use basic regular expression and extended regular expression matching, respectively, and in both cases the <period> in 0.1 matches any character. So, the RE 0.1 matches the text in red in the output:
Code:
 ./9.9tmp:0.609114
 ./9.9tmp:0.609114
 ./9.9tmp:0.609114
 ./91.9tmp:0.570516
 ./91.9tmp:0.570516
 ./92.9tmp:0.409131
 ./93.9tmp:0.904146
 ./93.9tmp:0.904146
 ./94.9tmp:0.609114
 ./97.9tmp:0.570516

but I have absolutely no explanation for why your first script:
Code:
find ./ -name "*.9tmp" -exec grep -H '0.1' {} +

did not also find:
Code:
 ./11.9tmp:0.179054
 ./11.9tmp:0.152542
 ./11.9tmp:0.152542
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.152542
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.179054
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465
 ./11.9tmp:0.176465

nor why id didn't report many of the files found by Aia's perl script.

My suggestion worked because grep -F 0.1 performs a fixed string search; not a regular expression search, and in a fixed string search the <period> in 0.1 only matches a <period>.

And, using grep -l just prints the name of a file that contains a match (without displaying the matching text) and moves on to the next file instead of looking for all possible matches in a single file.
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 02-21-2017
Quote:
Expanding a little on what I said in post #7, grep 0.1 and grep -E 0.1 use basic regular expression and extended regular expression matching, respectively, and in both cases the <period> in 0.1 matches any character. So, the RE 0.1 matches the text in red in the output:
Got it!
Quote:
but I have absolutely no explanation for why your first script:did not also find
Quote:
nor why id didn't report many of the files found by Aia's perl script.
Sorry, I just listed a small subset. I was not very clear in my following statement outputs (subset):
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