1

I am searching multiple words in single search like below

egrep -rin 'abc|bbc' folder

I am want to get an output with search keyword in the result like

abc:folder/1.txt:32:abc is here
bbc:folder/ss/2.txt:2:    bbc is here
Dyapa Srikanth
  • 1,231
  • 2
  • 22
  • 60
  • 1
    At least GNU(Linux+) and FreeBSD(Mac) versions of `grep` have `--color` (or `--colour`) which highlights the portion of the line matching the regexp in a different color if your terminal/emulator supports it -- which nearly all do today, although back in the '70s and '80s when people used a wide variety of real/hardware terminals many did not. Note for totally disjoint strings instead of `-E 'foo|bar'` you can use multiple nonextended regexps `-e foo -e bar` – dave_thompson_085 Jun 27 '21 at 10:56
  • Do you want the regexp you searched for or the string that matched the regexp printed? What should the output be if the regexp matches multiple times on a line? When you say `multiple words` does that mean you actually want to do full-word string comparisons instead of partial-line regexp comparisons as shown in your example? – Ed Morton Jun 27 '21 at 13:17
  • Please [edit] your question to include a [mcve] with concise, testable sample input and expected output so we can help you. Don't just show us the trivial, sunny day stuff - show us search strings that contain regexp metachars, matching "words" in the input that exist on their own as well as existing as substrings of other words, etc. See https://stackoverflow.com/questions/65621325/how-do-i-find-the-text-that-matches-a-pattern for more information on how to ask and create a [mcve] for a question like this. – Ed Morton Jun 27 '21 at 13:23

1 Answers1

1

Here's some ways:

  1. Post process after all the results:
grep -rinE 'abc|bbc' folder | sed '/abc/{s/^/abc:/; b}; s/^/bbc:/'

If there are many search terms:

$ for p in abc bbc; do echo "/$p/{s/^/$p:/; b};" ; done > script.sed
$ cat script.sed
/abc/{s/^/abc:/; b};
/bbc/{s/^/bbc:/; b};

$ grep -rinE 'abc|bbc' folder | sed -f script.sed

Note that this solution and the next one both will need attention if contents of the search terms can conflict with sed metacharacters.

  1. Post process for each search term:
# add -F option for grep if search terms are fixed string
# quote search terms passed to the for loop if it can contain metacharacters
for p in abc bbc; do grep -rin "$p" folder | sed 's/^/'"$p"':/'; done
  1. With find+gawk
$ cat script.awk
BEGIN {
    OFS = ":"
    a[1] = @/abc/
    a[2] = @/bbc/
}

{
    for (i = 1; i <= 2; i++) {
        if ($0 ~ a[i]) {
            print a[i], FILENAME, FNR, $0
        }
    }
}

$ find folder -type f -exec awk -f script.awk {} +
  • If you are searching for fixed strings:
    • Change array to a[1] = "abc" and a[2] = "bbc"
    • Change the condition to if (index($0, a[i]))
Sundeep
  • 23,246
  • 2
  • 28
  • 103
  • I am doing a bulk search in multiple folders, adding search term in loop will slow down the search & search terms will be dynamic – Dyapa Srikanth Jun 27 '21 at 05:09
  • 1
    @DyapaSrikanth there is no magic bullet, this is what I can think of. As far as I know, there's no built-in option in `grep` or alternate tools like `ripgrep` that can already do this. Use `ripgrep` for faster results since it has parallel processing built-in. – Sundeep Jun 27 '21 at 05:12
  • Ok, I will try this ripgrep – Dyapa Srikanth Jun 27 '21 at 22:46