Skip to main content
added 110 characters in body
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172

git grep

Here is the syntax using git grep combining multiple patterns using Boolean expressions:

git grep -e "life" --and -e "happy" --and -e "horse"

The above command will print lines matching all the patterns at once.

If the files aren't under version control, add --no-index param.

Search files in the current directory that is not managed by Git.

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

See alsoRelated: How to run grep with multiple AND patterns? and How to grep for 2 words existing on the same line?

git grep

Here is the syntax using git grep combining multiple patterns using Boolean expressions:

git grep -e "life" --and -e "happy" --and -e "horse"

The above command will print lines matching all the patterns at once.

If the files aren't under version control, add --no-index param.

Search files in the current directory that is not managed by Git.

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

See also: How to run grep with multiple AND patterns? and How to grep for 2 words existing on the same line?

git grep

Here is the syntax using git grep combining multiple patterns using Boolean expressions:

git grep -e "life" --and -e "happy" --and -e "horse"

The above command will print lines matching all the patterns at once.

If the files aren't under version control, add --no-index param.

Search files in the current directory that is not managed by Git.

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

Related:

added 48 characters in body
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172

git grep

Here is the syntax using git grep combining multiple patterns using Boolean expressions:

git grep -e "life" --and -e "happy" --and -e "horse"

The above command will print lines matching all the patterns at once.

If the files aren't under version control, add --no-index param.

Search files in the current directory that is not managed by Git.

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

See also: How to run grep with multiple AND patterns? and How to grep for 2 words existing on the same line?

git grep

Here is the syntax using git grep:

git grep -e "life" --and -e "happy" --and -e "horse"

If the files aren't under version control, add --no-index param.

Search files in the current directory that is not managed by Git.

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

See also: How to run grep with multiple AND patterns? and How to grep for 2 words existing on the same line?

git grep

Here is the syntax using git grep combining multiple patterns using Boolean expressions:

git grep -e "life" --and -e "happy" --and -e "horse"

The above command will print lines matching all the patterns at once.

If the files aren't under version control, add --no-index param.

Search files in the current directory that is not managed by Git.

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

See also: How to run grep with multiple AND patterns? and How to grep for 2 words existing on the same line?

added 48 characters in body
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172

git grep

Here is the syntax using git grep:

git grep -e "life" --and -e "happy" --and -e "horse"

If the file isn'tfiles aren't under version control, add it by:--no-index param.

git init
git add file.txt

Search files in the current directory that is not managed by Git.

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

See also: How to run grep with multiple AND patterns? and How to grep for 2 words existing on the same line?

git grep

Here is the syntax using git grep:

git grep -e "life" --and -e "happy" --and -e "horse"

If the file isn't under version control, add it by:

git init
git add file.txt

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

See also: How to run grep with multiple AND patterns? and How to grep for 2 words existing on the same line?

git grep

Here is the syntax using git grep:

git grep -e "life" --and -e "happy" --and -e "horse"

If the files aren't under version control, add --no-index param.

Search files in the current directory that is not managed by Git.

Check man git-grep for help.

grep

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns at the same time, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep "$patt" text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).

Another approach would be to remove all lines which does not match at least one pattern.

Here is the solution using Ex/Vim editor changing the file in-place:

while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt

Note: This will remove the unneeded lines from the file it-self.

Here is shorter version which will print the result on the screen only:

ex $(xargs -I% printf "+v/%/d " < args.txt) +%p -scq! text.txt

Change +%p -scq! to -scwq to save it in-place into the file.


And here is the solution by defining a shell alias:

alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < args.txt)"

Sample usage:

grep-all file.txt

See also: How to run grep with multiple AND patterns? and How to grep for 2 words existing on the same line?

Expands the answer.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
Loading
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link
Loading
added 113 characters in body
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
added 264 characters in body
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
added 224 characters in body
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
Expanding a bit.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
Corrects issue with the pattern has a space.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
Post Undeleted by kenorb
Corrects answer.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading
Post Deleted by kenorb
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172
Loading