Searching for a list of strings in a file with Python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for a list of strings in a file with Python
# 1  
Old 10-14-2013
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.
# 2  
Old 10-14-2013
Since you've posted in a shell scripting forum, I assume a shell solution is OK for you.

Code:
grep -f file_containing_strings output.csv


Last edited by balajesuri; 10-14-2013 at 04:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

3. Shell Programming and Scripting

Python: list with tuples to a file

I have few list value which are shown as below and want to print the same to a text file in python. But I couldn't able to write to a file. Could anyone please help me how to print the list with tuples to a file . My code: ====== fout=open('output.txt','w') mylist = fout.write()... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. UNIX for Dummies Questions & Answers

Grep -B used with -f? (Searching a file using a list of terms, output is lines before each match)

(1 Reply)
Discussion started by: Twinklefingers
1 Replies

5. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

6. Shell Programming and Scripting

Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents. -----cat 1.txt----- 1234 5678 1256 1234 1247 ------------------- I have 3 more files in a folder -----ls -lrt------- A1.txt A2.txt A3.txt ------------------- The contents of those three files are similar format with different data values... (8 Replies)
Discussion started by: realspirituals
8 Replies

7. Solaris

higlighting strings while searching

Hello experts, i am using sun solaris 9 i try to searching string from a file using more command. I wish when i search a string it will higlight the string/strings from the file. Have any idea how to do it..? I use putty. br//purple (9 Replies)
Discussion started by: thepurple
9 Replies

8. Shell Programming and Scripting

list and export strings in a file

Hi all, I have a file looks something like this: ~~~~~~~~~~~~~~~~~~~~~~~~~ aaaa bbbbbb ccc dddddddd ~~~~~~~~~~~~~~~~~~~~~~~~~ I will like a script that can echo line1 export line1 as env variable echo line2 export line2 as env variable etc etc Thanks in advance (3 Replies)
Discussion started by: Avatar Gixxer
3 Replies

9. UNIX for Dummies Questions & Answers

Searching list of entries in file for actual files in dir

Hi all, I have a file entries.txt that contains a number of entries all on seperate lines e.g. test1 test2 test3 test4 Then in a directory called /TestFiles I have a number of files that could contain the above text in the file name e.g. qwertytest1.csv qwertytest2.csv... (2 Replies)
Discussion started by: not4google
2 Replies

10. Shell Programming and Scripting

searching for filenames with search strings in another file

Hi, I have 5 files in a directory. emp1_usage.txt emp2_usage.txt emp3_usage.txt emp4_usage.txt emp5_usage.txt I am using sqlldr to get the contents of the above 5 files and store it in a temp table and update my original table using temp table. for f in *emp*.txt do sqlldr... (3 Replies)
Discussion started by: pathanjalireddy
3 Replies
Login or Register to Ask a Question