LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 01-08-2008, 02:18 AM   #1
horacioemilio
Member
 
Registered: Dec 2007
Posts: 61

Rep: Reputation: 15
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 ?

Thanks
 
Old 01-08-2008, 02:35 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
What have you tried so far ?
 
Old 01-08-2008, 02:38 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
What are you getting at ghostdog74 ??? ....

Gotta be do-able in python. Me, I'd use perl.
 
Old 01-08-2008, 02:43 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by syg00 View Post
What are you getting at ghostdog74 ??? ....
not sure. However, there ought to be an option to delete my own posts.
Quote:
Gotta be do-able in python. Me, I'd use perl.
Yes, of course there is.
 
Old 01-08-2008, 02:46 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
this is crazy. can some mod/admin help to delete them for me. thanks
 
Old 01-08-2008, 05:52 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 01-08-2008, 07:48 AM   #7
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Duplicate posts removed.
 
Old 01-08-2008, 07:55 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
and grep can be called from Python.....
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.
 
Old 01-08-2008, 08:28 AM   #9
horacioemilio
Member
 
Registered: Dec 2007
Posts: 61

Original Poster
Rep: Reputation: 15
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 ?

thanks
 
Old 01-08-2008, 08:40 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ghostdog74 View Post
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)
 
Old 01-08-2008, 09:06 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This is a oneliner in awk:
Code:
awk  '/stringOrRegexToFind/ { print NR;}' fileToSearch.txt
--- rod.

EDIT: Oops. Didn't notice that this is a python question.

Last edited by theNbomr; 01-08-2008 at 09:08 AM.
 
Old 01-08-2008, 09:30 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
Of course.....it was a feeble attempt at humor.
oh. ok. lol
Quote:
(Why use Python when the simple grep will work)
is this humor as well? Because if its not, there are many reasons why.
 
Old 01-08-2008, 09:33 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by horacioemilio View Post
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.
 
Old 01-08-2008, 09:55 AM   #14
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
import sys
i = 1
for line in file(sys.argv[2]):
    if line.find(sys.argv[1]) >= 0: print i
    i += 1
Run this script like grep and it will print the number of the line where it found "hko" in /etc/passwd.

Example:
Code:
bash$ python scriptname.py hko /etc/passwd
28
bash$

Last edited by Hko; 01-08-2008 at 10:07 AM.
 
Old 01-08-2008, 04:22 PM   #15
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Code:
perl -lane 'print "$.:$_" if /text/' file
Perl mimicking grep -n.

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.
 
  


Reply



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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
how to get the number of line of a file? loplayers Linux - Newbie 2 10-24-2007 04:10 AM
Script to insert string in first line of a file minil Programming 13 01-02-2006 11:56 PM
Substitute String or line in a file dimsh Linux - Newbie 4 09-21-2005 03:26 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:18 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration