getting the line just before or after a pattern searched

S

s99999999s2003

hi

i have a file something like this

abcdefgh
ijklmnopq
12345678
rstuvwxyz
......
......
......
12345678
......

whenever i search the file and reach 12345678, how do i get the line
just above and below ( or more than 1 line above/below) the pattern
12345678 and save to variables? thanks
 
A

Alex Martelli

hi

i have a file something like this

abcdefgh
ijklmnopq
12345678
rstuvwxyz
.....
.....
.....
12345678
.....

whenever i search the file and reach 12345678, how do i get the line
just above and below ( or more than 1 line above/below) the pattern
12345678 and save to variables? thanks

If the file's of reasonable size, read it all into memory into a list of
lines with a .readlines method call, then loop with an index over said
list and when you find your pattern at index i use i-1, i+1, and so on
(just check that the resulting i+N or i-N is >=0 and <len(thelist)).

If the file's too big to keep in memory at once, you need more clever
approaches -- keep a collections.deque of the last M lines read to be
able to get "N lines ago" for N<M, and for getting lines "after" the one
of interest (which you will read only in "future" iterations) keep track
of relative linenumbers that "will" interest you, decrement them with
each line read, trigger when they reach 0. But unless you're dealing
with files of many, MANY hundres of megabytes, on any typical modern
machine you should be OK with the first, WAY simpler approach.


Alex
 
R

Raymond Hettinger

hi

i have a file something like this

abcdefgh
ijklmnopq
12345678
rstuvwxyz
.....
.....
.....
12345678
.....

whenever i search the file and reach 12345678, how do i get the line
just above and below ( or more than 1 line above/below) the pattern
12345678 and save to variables? thanks

You could use the re module to search everything at once:
import re
print re.findall('\n([^\n]*)\n12345678\n([^\n]*)', open('input.dat').read())
[('ijklmnopq ', 'rstuvwxyz '), ('..... ', '.....')]


Raymond
 
D

Daniel Marcel Eichler

i have a file something like this

abcdefgh
ijklmnopq
12345678
rstuvwxyz
.....
.....
.....
12345678
.....

whenever i search the file and reach 12345678, how do i get the line
just above and below ( or more than 1 line above/below) the pattern
12345678 and save to variables? thanks

source = file(bla).read().split('\n')
for i, line in enumerate(source):
if line == '12345678':
print '\n'.join( source[i-1:i+1] )

Something like this, for example. Of course, you must also secure that i-1
isn't smaller than zero.


mfg

Daniel
 
M

Magnus Lycka

hi

i have a file something like this

abcdefgh
ijklmnopq
12345678
rstuvwxyz
.....
.....
.....
12345678
.....

whenever i search the file and reach 12345678, how do i get the line
just above and below ( or more than 1 line above/below) the pattern
12345678 and save to variables? thanks

For gigantic files (i.e. can't put all in RAM at once)
just make sure you always remember the previously read line.
Something like this:

old = None
f = open('mybigfile.txt')
for line in f:
if line == '12345678\n':
print old,
print line,
print f.next(),
old=line
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top