Search multiple strings for multiple words

grep multiple strings and condition
how to grep multiple words in a single line in unix
zgrep multiple strings
grep multiple strings r
bash find multiple strings in file
how to search a particular word in a file in linux
grep two words both
grep either of two words

I have a dataframe containing a sentence per row. I need to search through these sentences for the occurence of certain words. This is how I currently do it:

import pandas as pd

p = pd.DataFrame({"sentence" : ["this is a test", "yet another test", "now two tests", "test a", "no test"]})

test_words = ["yet", "test"]
p["word_test"] = ""
p["word_yet"]  = ""

for i in range(len(p)):
    for word in test_words:
        p.loc[i]["word_"+word] = p.loc[i]["sentence"].find(word)

This works as intended, however, is it possible to optimize this? It runs fairly slow for large dataframes

You can use str.find

p['word_test'] = p.sentence.str.find('test')
p['word_yet'] = p.sentence.str.find('yet')

    sentence         word_test  word_yet    word_yest
0   this is a test   10         -1          -1
1   yet another test 12          0          0
2   now two tests    8          -1          -1
3   test a           0          -1          -1
4   no test          3          -1          -1

Search Multiple Words / String Pattern Using grep command, In this quick tutorial, we'll find out how to detect multiple words inside of a string. 2​. Our Example. Search Multiple Words or String Patterns Using grep Command. The grep command used to find a particular string or pattern in one or multiple files. Grep stands for “Global Regular Expression Print”. Grep is the most powerful command in Linux. It can be also used to read Standard Output from another command.

IIUC, use a simple list comprehension and call str.find for each word:

u = pd.DataFrame({
    # 'word_{}'.format(w)
    f'word_{w}': df.sentence.str.find(w) for w in test_words}, index=df.index)
   word_yet  word_test
0        -1         10
1         0         12
2        -1          8
3        -1          0
4        -1          3

pd.concat([df, u], axis=1)

           sentence  word_yet  word_test
0    this is a test        -1         10
1  yet another test         0         12
2     now two tests        -1          8
3            test a        -1          0
4           no test        -1          3

Check If a String Contains Multiple Keywords, Learn to Grep Search Multiple Words / String Patterns (Bash Shell) - Grep is one of the most powerful command-line utility in Linux. It is used to  I have multiple word documents in a folder and i want to search specific text in them. The windows file explorer does more or less this but it has something like a fuzzy search. It might match one word and a sentence and show it as a result, however i want to search for specific words, sentences.

Since you mentioned better performance using np.char.find

df=pd.DataFrame(data=[np.char.find(p.sentence.values.astype(str),x) for x in test_words],index=test_words,columns=p.index)
           sentence  yet  test
0    this is a test   -1    10
1  yet another test    0    12
2     now two tests   -1     8
3            test a   -1     0
4           no test   -1     3

Grep Search Multiple Words / String Patterns [ Bash Shell ], Grep Multiple Strings #. Literal strings are the most basic patterns. In the following example, we are searching for all occurrences of the words  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to search for multiple strings in a file

How to Grep for Multiple Strings and Patterns, If I understand what you are asking, it is a regular expression like this: ^(8768|​9875|2353). This matches the three sets of digit strings at  The straightforward solutions take O (S* (A^2)), where S is the length of str and A is the sum of the lenghts of all strings in a. For a faster solution, look at Aho-Corasick algorithm for string matching, which runs in linear time O (S+A).

Regular expression to search multiple strings (Textpad), I assume (always dangerous) that you want to find whole words, so "test" would match but "testy" would not. Thus the pattern must search for  How to check if multiple strings exist in another string in Python? Python Server Side Programming Programming To check if any of the strings in an array exists in another string, you can use the any function.

Multiple words in any order using regex, 5.2. Find Any of Multiple Words Problem You want to find any one out of a list of words, without having to search through the subject string multiple times. Macro highlights multiple search strings The manual way is fine and it might be all you need. However, it's limited: You can search for only one string and the highlights are temporary. If you