getting the line just before or after a pattern searched

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s99999999s2003@yahoo.com

    #1

    getting the line just before or after a pattern searched

    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

  • Alex Martelli

    #2
    Re: getting the line just before or after a pattern searched

    <s99999999s2003 @yahoo.com> wrote:
    [color=blue]
    > 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[/color]

    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.deq ue 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

    Comment

    • bonono@gmail.com

      #3
      Re: getting the line just before or after a pattern searched


      s99999999s2003@ yahoo.com wrote:[color=blue]
      > 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[/color]

      I would try something similar to the pairwise recipe here :



      Comment

      • Raymond Hettinger

        #4
        Re: getting the line just before or after a pattern searched

        s99999999s2003@ yahoo.com wrote:[color=blue]
        > 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[/color]

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


        Raymond

        Comment

        • Daniel Marcel Eichler

          #5
          Re: getting the line just before or after a pattern searched

          s99999999s2003@ yahoo.com wrote:
          [color=blue]
          >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[/color]

          source = file(bla).read( ).split('\n')
          for i, line in enumerate(sourc e):
          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

          Comment

          • Magnus Lycka

            #6
            Re: getting the line just before or after a pattern searched

            s99999999s2003@ yahoo.com wrote:[color=blue]
            > 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[/color]

            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

            Comment

            Working...