LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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:

Code:
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
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,328
Blog Entries: 33

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
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.

Code:
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
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,195

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
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.
 
  


Reply

Tags
python


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 04:19 PM.

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