Look for a string on a file and get its line number
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Look for a string on a file and get its line number
Hi,
I have to search for a string on a big file. Once this string is found, I would need to get the number of the line in which the string is located on the file. Do you know how if this is possible to do in python ?
Well, there is "grep -n", and grep can be called from Python.....
The brute force way (in any language) would be to set up a loop with a line counter and then enclose a command that reads a line and tests for the string.
usually, one shouldn't do that since feature rich languages like Python/Perl(and others) have inbuilt string and file manipulation utilities to do what grep can. Anyway, OP has got some answers in another forum, but just for the record.
Code:
#!/usr/bin/env python
for num,line in enumerate(open("file")):
if "search_string" in line: print num
Last edited by ghostdog74; 01-08-2008 at 07:56 AM.
Hi, thanks for the help. Then I got running the following code;
Code:
#!/usr/bin/env python
import os, sys, re, string, array, linecache, math
nlach = 12532
lach_list = sys.argv[1]
lach_list_file = open(lach_list,"r")
lach_mol2 = sys.argv[2] # name of the lachand mol2 file
lach_mol2_file = open(lach_mol2,"r")
n_lach_read=int(sys.argv[3])
# Do the following for the total number of lachands
# 1. read the list with the ranked lachands
for i in range(1,n_lach_read+1):
line = lach_list_file.readline()
ll = string.split (line)
#print i, ll[0]
lach = int(ll[0])
# 2. for each lachand, print mol2 file
# 2a. find lachand header in lachand mol2 file (example; kanaka)
# and return line number
line_nr = 0
for line in lach_mol2_file:
line_nr += 1
has_match = line.find('kanaka')
if has_match >= 0:
print 'Found in line %d' % (line_nr)
# 2b. print on screen all the info for this lachand
# (but first need to read natoms and nbonds info)
# go to line line_nr + 1
ltr=linecache.getline(lach_mol2, line_nr + 1)
ll=ltr.split()
#print ll[0],ll[1]
nat=int(ll[0])
nb=int(ll[1])
# total lines to print:
# header, 8
# at, na
# b header, 1
# n
# lastheaders, 2
# so; nat + nb + 11
ntotal_lines = nat + nb + 11
# now we go to the beginning of the lachand
# and print ntotal_lines
for j in range(0,ntotal_lines):
print linecache.getline(lach_mol2, line_nr - 1 + j )
which almost works. In the last "for j" loop, i expected to obtain an output like:
sdsdsdsdsdsd
sdsdsfdgdgdgdg
hdfgdgdgdg
but instead of this, i get:
sdsdsdsdsdsd
sdsdsfdgdgdgdg
hdfgdgdgdg
and also the program is very slow. Do you know how could i solve this ?
usually, one shouldn't do that since feature rich languages like Python/Perl(and others) have inbuilt string and file manipulation utilities to do what grep can.
Of course.....it was a feeble attempt at humor. (Why use Python when the simple grep will work)
and also the program is very slow. Do you know how could i solve this ?
thanks
provide your sample input file, describe what you want to do and how your expected output will look like. There are definitely betters ways to do what you want.
Edit: DOH! just realized it was python question... hmm well since I've already posted I guess I gotta contribute somehow to not look foolish.
umm...ooh, regex support!
Code:
import sys
import re
for i, line in enumerate(open(sys.argv[2])):
if re.search(sys.argv[1], line): print "%s:%s" %(i, line)
that mimics grep -n, and uses regex.
Edit: changed from re.match() to re.search()
Last edited by angrybanana; 01-08-2008 at 04:37 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.