Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-14-2013, 12:34 AM   #1
LQ Newbie
Registered: Jan 2011
Posts: 11

Rep: Reputation: 0
Searching for a list of strings in a file with Python

Hi guys,

I'm trying to search for several strings, which I have in a .txt file line by line, on another file.
So the idea is, take input.txt and search for each line in that file in another file, let's call it rules.txt.

So far, I've been able to do this, to search for individual strings:

import re

shakes = open("output.csv", "r")

for line in shakes:
    if re.match("STRING", line):
        print line,
How can I change this to input the strings to be searched from another file?

So far I haven't been able to.

Thanks for the ideas.
Old 10-14-2013, 12:46 AM   #2
Senior Member
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,850
Blog Entries: 36

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
Is this homework? Is this something you're trying to accomplish for a particular job? Or are you just playing around with python and learning it? What's your real question in addition to this one?

If you need to search for strings in files in a tree structure then grep is the utility you should us.

grep -irl 'some string' somedirctory/
See the man page for an explanation of the options.

Last edited by sag47; 10-14-2013 at 12:48 AM.
Old 10-14-2013, 03:19 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
As mentioned there are always better tools to be considered, but assuming it must be Python, I would suggest opening the input.txt file in the same manner and then storing
each string in something like a list / array and then on opening rules.txt you can check each line against the data contained in the list / array.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
suing sed to remove certain list of strings from a file ??? HuMan-BiEnG Linux - Distributions 11 10-13-2010 09:04 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
Searching .txt file for (specific) strings and printing them to new file Hb_Kai Linux - General 7 02-18-2010 09:09 AM
Python: replacing strings in a file tonya11en Programming 2 05-19-2009 06:52 PM
searching contents of .class file within jar (for strings) wakeboarder3780 Linux - Newbie 4 02-17-2009 01:49 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration