Grep Search All Files in Directory for String1 and String2

Grep Search all files in directory for string1 AND string2


grep -r db-connect.php . | grep version

grep Command for Files with String1 AND NOT String2

You can use the -L flag:

-L, --files-without-match  print only names of FILEs containing no match

First you find the files that doesn't contain "String2", then you run on those and find the ones that does contain "String1":

$ grep -L "String2" * | xargs grep -nH "String1"

Recursively locate all files that have string a AND string b using grep

Pipe the results of your first search to grep again:

grep -RlZ "String1" . | xargs -0 grep -l "String2"

This would list the files containing both String1 and String2.

Getting the line numbers for the files containing both the strings wouldn't be probably very efficient since you need to know that a priori. One way would be to again pipe the results to grep:

grep -RlZ "String1" . | xargs -0 grep -lZ "String2" | xargs -0 grep -En 'String1|String2'

Find all files containing two patterns

This will do, even when the strings are not on the same line:

find ~ -type f -name \*.txt -exec grep -iq string1 \{\} \; -exec grep -iq string2 \{\} \; -print

Find files containing multiple strings

you can also try this;

find . -type f -exec grep -l 'string1' {} \; | xargs grep -l 'string2'

this shows file names that contain string1 and string2

Finding multiple strings in directory using linux commends

I normally use the following to search for strings inside my source codes. It searches for string and shows the exact line number where that text appears. Very helpful for searching string in source code files. You can always pipes the output to another grep and filter outputs.

grep -rn "text_to_search" directory_name/

example:

$ grep -rn "angular" menuapp
$ grep -rn "angular" menuapp | grep some_other_string

output would be:

menuapp/public/javascripts/angular.min.js:251://# sourceMappingURL=angular.min.js.map
menuapp/public/javascripts/app.js:1:var app = angular.module("menuApp", []);

Regex/Grep to filter strings in text file

You can use a single pattern matching either word or excel or an empty line using an alternation and check if there is a reversed result, and use --quiet to not write to standard output.

if grep --quiet -v 'word\|excel\|^[[:space:]]*$' document.txt; then
echo "error"
fi


Related Topics



Leave a reply



Submit