#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    20
    Rep Power
    0

    Extract lines from file


    Hi..

    I have a large file (input.txt) with values in following format:

    OG1: or10|1345 or10|387 or10|474 or11|1203 or11|182 or10|2158 or12|637
    OG2: or10|1562 or10|1584 or10|1977 or11|2263 or11|43
    OG3: or12|2400 or12|2401 or13|2697 or13|2698 or16|2 or16|914 or27|1355
    OG4: or10|108 or20|2713 or25|2315 or25|2754 or2|1411

    …………..
    ………

    From this file, I want to find how many times ‘or10’ appear in different OGs (and also the corresponding number after pipe) and paste it in a output file (output.txt).. Here the output would be:

    OG1: or10|1345 or10|387 or10|474 or10|2158
    OG2: or10|1562 or10|1584 or10|1977
    OG3:
    OG4: or10|108

    Any idea? Thanks in advance..
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Well, this is untested but it should be close:
    Code:
    import re
    fid=open(<filename>)
    count=0
    afterpipe=[]
    for a in fid:
        if a.count("or10")>0
            count+=a.count("or10")
            afterpipe.append(re.findall(r'or10|\d{4}',a))
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    20
    Rep Power
    0
    Gives syntax error

    if a.count("or10")>0
    ^
    SyntaxError: invalid syntax



    Originally Posted by rrashkin
    Well, this is untested but it should be close:
    Code:
    import re
    fid=open(<filename>)
    count=0
    afterpipe=[]
    for a in fid:
        if a.count("or10")>0
            count+=a.count("or10")
            afterpipe.append(re.findall(r'or10|\d{4}',a))
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Originally Posted by abhijit.bose
    Gives syntax error

    if a.count("or10")>0
    ^
    SyntaxError: invalid syntax
    I forgot the ":" at the end of the if statement:
    if a.count("or10"):
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    20
    Rep Power
    0
    Sorry but still gives error:

    if a.count("or10"):>0
    ^
    SyntaxError: invalid syntax

    Originally Posted by rrashkin
    I forgot the ":" at the end of the if statement:
    if a.count("or10"):
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by abhijit.bose
    Sorry but still gives error:

    if a.count("or10"):>0
    ^
    SyntaxError: invalid syntax
    Code:
    if a.count("or10") > 0:

IMN logo majestic logo threadwatch logo seochat tools logo