Grep usage

From raju

Exclude lines with tab

    grep -vP "\t" database.tsv
    

Ref:- https://askubuntu.com/questions/53071/how-to-grep-for-tabs-without-using-literal-tabs-and-why-does-t-not-work

Some favorite options

grep -inH --color

grep files that contain multiple words on different lines

The idea here is to list all the files containing all the keywords but not necessarily on the same line.

  • Solution 1:

If git is installed

    git grep -l --all-match --no-index -e word1 -e word2 -e wordN
    

The --no-index searches files in the current directory that is not managed by Git. So this command will work in any directory irrespective of whether it is a git repository or not.

By default the search is recursive. Use --max-depth or specify file glob to restrict it further.

Example:

    git grep -l --all-match --no-index -e continuous -e discrete -e nominal -e ordinal *.txt
    

Ref:- https://stackoverflow.com/a/55926788/6305733

  • Solution 2:

A pure grep solution

    grep -lZ word1 * | xargs -0 grep -lZ word2 | xargs -0 grep -lZ word3 | xargs -0 grep -l wordN
    

Note:- All grep commands have the -Z flag except the last.

Scope:- The command will work even if the filenames contain spaces, newline characters etc.,

Example:- To list all the txt files that contain the words continuous, discrete, nominal, ordinal somewhere in the file.

    grep -lZ continuous *txt | xargs -0 grep -lZ discrete | xargs -0 grep -lZ nominal | xargs -0 grep -l ordinal
    

If the filenames do not contain newline characters but contain spaces

    grep -l word1 * | xargs -d '\n' grep -l word2 | xargs -d '\n' grep -l word3 | xargs -d '\n' grep -l wordN
    

If the filenames do not have any special characters such as as spaces, newline characters

    grep -l word1 * | xargs grep -l word2 | xargs grep -l word3 | xargs grep -l wordN
    

Ref:- https://stackoverflow.com/questions/4795323/grep-for-multiple-strings-in-file-on-different-lines-ie-whole-file-not-line-b

multiple words

Show results from files only if they contain all the keywords but not necessarily on the same line

    git grep --all-match --no-index -e string1 -e string2 -e string3 file
    

The --no-index searches files in the current directory that is not managed by Git. So this command will work in any directory irrespective of whether it is a git repository or not.

Ref:- https://stackoverflow.com/a/55926788/6305733

To grep for files that contain any of multiple keywords:

$ grep --color -E "word1|word2|word3" *.txt

To grep for files that contain all words:

    git grep -l --all-match --no-index -e word1 -e word2 -e wordN *.txt
    

More details: grep for files containing words on different lines

To grep for files that contain multiple words (without any order) in one line:

$ grep word1 *.txt | grep word2 | grep word3

To grep for files that contain multiple words in one line in a given order:

$ egrep '(word1.*word2.*word3)' *

Ref:- http://xmodulo.com/how-to-grep-multiple-terms-or-strings.html

grep and view

open all files containing a keyword

$ grep keyword *.pl -l | xargs gvim -p

Exclude .git directory from grep searches

grep and xargs

    grep -Z | xargs -0
    

This will use '\0' as the delimiter.

search tags | xargs do not break on space, grep equivalent of "find -print0"

To run xargs with '\n' as the delimiter

    blahblah | xargs -d '\n' -l1
    

Ref:- https://unix.stackexchange.com/questions/256522/how-to-make-xargs-handle-spaces-and-special-chars-from-cat