Trying to search case insensitive keywords from a log text (.txt) file

grep case insensitive search
ignore case sensitive in unix shell script
case insensitive search in unix
vi search case insensitive
linux case sensitive disable
how to do case insensitive search in unix
case insensitive search in bash
bash check if (( $string contains substring case insensitive))

I have a log file of a conversation. I want to search the file for certain keywords which I have assigned but the log file may contain uppercase, lowercase and title case sensitive words of the keyword I am searching.

I can pull outlines which have the keyword in lower case but can't get the uppercase or title case versions of the word. How can I solve this?

I have tried using

if (words.title() and words.lower()) in line:
     print (searchInLines[i])

but that doesn't seem to work.

keywords=['bimbo', 'qualified', 'tornadoes', 'alteryx', 'excel', 'manchester']


with open("recognition_log.txt", "r", encoding="utf8") as f:
    searchInLines = f.readlines()
    f.close()

for words in keywords:
    for i, line in enumerate(searchInLines):
        if (words.title() and words.lower()) in line:
            print (searchInLines[i])

For example, the log file contains the following sentence:

"Manchester United played Barcelona yesterday, however, the manchester club lost"

I have "manchester" in my keywords so it will pick up the second one but not the first one.

How can I recognise both?

Thanks in Advance!

I was not entirely sure what you were trying to do, but I assume it is filtering out messages (lines) that contains one of the words in keywords. Here is a simple way of doing it:

keywords=['bimbo', 'qualified', 'tornadoes', 'alteryx', 'excel', 'manchester']

with open("recognition_log.txt", "r", encoding="utf8") as f:
    searchInLines = f.readlines()
    f.close()

for line in searchInLines:
    for keyword in keywords:
        if keyword in line.lower():
            print(line)

Linux: Case-insensitive file searching with locate and find , While trying to remember where I put it I realized I was going to have to do some case-insensitive file searching. I was happy to learn that both of� While trying to remember where I put it I realized I was going to have to do some case-insensitive file searching. I was happy to learn that both of my favorite Unix and Linux file-finding utilities support case-insensitive file searching. Both the find command and the locate command have command-line options that provide this support.

Using Regex

Ex:

import re

keywords=['bimbo', 'qualified', 'tornadoes', 'alteryx', 'excel', 'manchester']


with open("recognition_log.txt", "r", encoding="utf8") as f:
    searchInLines = f.readlines()

#pattern = re.compile("(" + "|".join(keywords) + ")", flags=re.IGNORECASE)
pattern = re.compile("(" + "|".join(r"\b{}\b".format(i) for i in keywords) + ")", flags=re.IGNORECASE)
for line in searchInLines:
    if pattern.search(line):
        print(line)

Finding Things – The Unix Shell, Use grep to select lines from text files that match simple patterns. By default, grep searches for a pattern in a case-sensitive way. She wants to write a shell script that takes a species as the first command-line argument and a directory as � To search for hello there in file x.y, type: findstr /c:hello there x.y To find all occurrences of the word Windows (with an initial capital letter W) in the file proposal.txt, type: findstr Windows proposal.txt To search every file in the current directory and all subdirectories that contained the word Windows, regardless of the letter case, type:

First of all, you dont need f.close() when you working with context manager.

As for solution, i recommend you to use regexp in that case

import re
keywords=['bimbo', 'qualified', 'tornadoes', 'alteryx', 'excel', 'manchester']
# Compiling regext pattern from keyword list
pattern = re.compile('|'.join(keywords))

with open("recognition_log.txt", "r", encoding="utf8") as f:
    searchInLines = f.readlines()

for line in searchInLines:
    # if we get a match
    if re.search(pattern, line.lower()):
        print(line)

Searching text strings from files using Python, Searching text strings from files in a given folder is easily Log into your account program to make exe file Figure 2: Option -s, which is case-sensitive try: if args.m: dir = args.m[1]. obj1 = Text_search(args.m[0], dir ). obj1.txt_search_m(). If you select option –s, then it will call the method txt_search(). Find text with grep using case insensitive option. In some cases, you may not be sure if the text is written with uppercase or lowercase letters. Luckily for you, the grep command has an option in order to search for text in files using a case insensitive option. To search for text using the case insensitive option, simply use the “-i” option.

You can convert both the line and the keywords to upper or to lower case and compare them.

keywords = ['bimbo', 'qualified', 'tornadoes', 'alteryx', 'excel', 'manchester']

with open("test.txt", "r", encoding="utf8") as f:
    searchInLines = f.readlines()
    f.close()

for words in keywords:
    for i, line in enumerate(searchInLines):
        if words.upper() in line.upper():
            print(searchInLines[i])

Search Multiple Words / String Pattern Using grep command, How do I search multiple strings or words using the grep command? Finally, try on older Unix shells/oses: grep -e pattern1 -e pattern2 *.pl In this example, search warning, error, and critical words in a text log file called I recommend that you pass the -i (ignore case) and --color option as follows too: The program below reads a log file line by line. If the line contains the word "error," it is added to a list called errors. If not, it is ignored. The lower() string method converts all strings to lowercase for comparison purposes, making the search case-insensitive without altering the original strings.

(1) Well, your words are in lower case, so "words.lower()" has no effect. (2) your example sentence would not be found if you wouldn't have "Manchester" AND "manchester" in it, since you are using "and" logic. (3) What you want, I believe, is: "if words in line.lower():"

Finding a File Containing a Particular Text String In Linux Server , I have to admit that there are tens and thousands of text files on any Linux or Trying to find all files containing specific text on my Linux desktop Our final example ignore case distinctions in both the search var/adm/log/secret.log file in the system and search a (case insensitive) pattern using grep. elif args.mi: dir = args.mi[1] obj1 = Text_search(args.mi[0],dir,i=1) obj1.txt_search_m() If you select option –s, then it will call the method txt_search() in case-insensitive mode. elif args.si: if args.si[1]: dir = args.si[1] obj1 = Text_search(args.si[0],dir,i=1) obj1.txt_search() If you select the –r option, then it will call the txt

Findstr command examples and regular expressions, For example, to search for the string 'Windows' in the text file CLItips.txt, the So if you run 'findstr windows /I CLItips.txt', it does case insensitive pattern matching. This command searches for the pattern in all *.log files, and then prints only the And i tried doing that and it is giving the a.bat file but one more doubt for me� Interestingly, Locate32 DOES do a case sensitive search on mp3 tags, now I've tried it. Well, well. Whereas Ultrasearch help says: Case sensitivity Differences in capital or smaller case letters will be disregarded for all search types (searching for file names, file types or file content). The search is 'case insensitive'.

Case-insensitive search in awk, awk 'tolower($0) ~ /&&&key word&&&/ { p = ! p ; next }; p' text.txt. You need single quotes because of the $0 , the BEGIN block can be removed� FILE - Zero or more input file names. To be able to search the file, the user running the command must have read access to the file. Search for a String in Files # The most basic usage of the grep command is to search for a string (text) in a file. For example, to display all the lines containing the string bash from the /etc/passwd file, you

15 Practical Grep Command Examples In Linux / UNIX, Case insensitive search using grep -i cat test-file.txt a b c d $ grep -v -e "a" -e "b " -e "c" test-file.txt d But it becomes very useful when you give a regex pattern and trying to see what it matches as I have a log file with identified errors. if we want to find a particular word from a text file what command� Case insensitive search in Vim. We can also do case insensitive search in Vim using the following two config options. For example, ignore case in search patterns, type::set ignorecase Now search it to match Foo, foo, FOO and so on: /foo Another option is to override the ‘ignorecase’ option if the search pattern contains upper case characters.

Comments
  • Possible duplicate of How to make string check case insensitive in Python 3.2?
  • Not sure why this got downvoted +1. You might want to put word boundaries around the alternation.