简体   繁体   中英

bash use output of find with grep

I'm trying to grep a pattern in an unknown file in a directory that has a lot of files. I'd normally execute the following code:

$ grep 0RY7HYLA in ./*8017*/*diag*

However because I'm searching for files in a very large folder of files I can't use grep directly because that operation consumes too much bandwidth. I need to use the find command in conjunction with grep. I'm struggling to pipe the output of the find command as the input to the grep command ie

find ./*8017*/*diag* -maxdepth 3 -type f -name output_log -exec egrep -l '0RY7HYLA' {} \;  | grep 0RY7HYLA

The above code does not work ie there is no output from running this code. If I only execute the code before the pipe it output the file and directory, but with the grep there is no output.

Secondly I need to put this in a conditional statement, something like:

if find ./*8017*/*diag* -maxdepth 3 -type f -name output_log -exec egrep -l '0RY7HYLA' {} \; ; then grep 0RY7HYLA ???????? ; fi

Is this doable?

If I understood it correctly I think that what you are trying to do could be solved with the following command:

find . -type f -print0 | xargs -0 grep "0RY7HYLA"

This will print the occurrences of "0RY7HYLA" in the files under the current directory.

I have to locate strings in a folder full of logs using bash and I do this in the following manner:

grep --color -r -i "string" \root\...\mostparentfoldertolookin\*

If you need to find multiple strings I would use the follwoing syntax:

egrep --color -r -i "string1|string2|string3" \root\...\mostparentfoldertolookin\*

The results produced in the following syntax:

\root\...\mostparentfoldertolookin\folderfileisin\file.log:<line the string is found on highlighting the string it's self>

If all you're looking for is the file that contains the string you can take the output up to the ":" as follows:

grep --color -r -i "string" \root\...\mostparentfoldertolookin\* | sed 's/:.*//'

Please comment if you require any further help, thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM