To remove some lines from a file

Discussion in 'Python' started by umut.tabak@student.kuleuven.be, Oct 25, 2006.

  1. Guest

    Dear all,

    I am quite new to python. I would like to remove two lines from a file.
    The file starts with some comments, the comment sign is $. The file
    structure is

    $
    $
    $
    $
    $
    $
    $
    ..
    ..
    ..
    ..
    $
    line1
    line 2
    $
    $
    ..
    ..
    ..
    $
    actual commands

    I would like to delete those two lines at the very beginning of the
    file between two comment blocks. I have read things about python but my
    library knowledge is still weak. So could you please help me with this
    code.

    I would like to call this as

    myscript targetfile

    I have read sth about command line arguments. Perhaps I can cope with
    that myself but help is also appreciated on that.

    Best regards,
    , Oct 25, 2006
    #1
    1. Advertising

  2. Jerry Guest

    Very inelegant, but you get the idea:

    counter = 0

    f = open("test.txt")
    for line in f.readlines():
    if line[0] != "$" and counter < 2:
    counter += 1
    continue
    else:
    print line,

    --
    Jerry
    Jerry, Oct 25, 2006
    #2
    1. Advertising

  3. Guest

    Thanks, I have learnt sth from the library BTW.

    Regards
    On Oct 25, 3:32 pm, "Jerry" <> wrote:
    > Very inelegant, but you get the idea:
    >
    > counter = 0
    >
    > f = open("test.txt")
    > for line in f.readlines():
    > if line[0] != "$" and counter < 2:
    > counter += 1
    > continue
    > else:
    > print line,
    >
    > --
    > Jerry
    , Oct 25, 2006
    #3
  4. wrote:
    > Dear all,
    >
    > I am quite new to python. I would like to remove two lines from a file.


    The canonical way to do so (at least for text files - which is the case
    here) is :

    open the source file in read mode
    open a tmp file in write mode

    for each line in source file:
    if this line should be kept:
    write it to the tmp file

    close both files
    replace source file with tmp file

    (snip)

    > I would like to call this as
    >
    > myscript targetfile
    >
    > I have read sth about command line arguments. Perhaps I can cope with
    > that myself but help is also appreciated on that.


    import sys
    print sys.argv


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    Bruno Desthuilliers, Oct 25, 2006
    #4
  5. wrote:
    > ... I would like to remove two lines from a file.
    > ...


    I am quite new myself -- but wouldn't grep -v do that easier (and
    perhaps faster)?

    Greetings,
    Sebastian.
    Sebastian Busch, Oct 25, 2006
    #5
  6. Steve Holden Guest

    Sebastian Busch wrote:
    > wrote:
    >
    >>... I would like to remove two lines from a file.
    >>...

    >
    >
    > I am quite new myself -- but wouldn't grep -v do that easier (and
    > perhaps faster)?
    >

    Kindly show how to use grep to solve this problem:

    Remove the first two lines that don't begin with "@" from a file.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
    Steve Holden, Oct 25, 2006
    #6
  7. Steve Holden wrote:
    > Sebastian Busch wrote:
    >> wrote:
    >>> ... I would like to remove two lines from a file. ...

    >> ... grep -v ...

    > ... show ...


    grep -v "`grep -v "commentsymbol" yourfile | head -2`" yourfile


    i frankly admit that there is also 'head' invoved ;)

    i really have no idea -- but i always thought that these coreutils and
    colleagues do their jobs as fast as possible, in particular faster than
    interpreted languages... however, as i posted last time, i was actually
    not aware that you have to call three of them.

    sebastian.
    Sebastian Busch, Oct 26, 2006
    #7
  8. Chetan Guest

    Sebastian Busch <> writes:

    > Steve Holden wrote:
    >> Sebastian Busch wrote:
    >>> wrote:
    >>>> ... I would like to remove two lines from a file. ...
    >>> ... grep -v ...

    >> ... show ...

    >
    > grep -v "`grep -v "commentsymbol" yourfile | head -2`" yourfile
    >
    >
    > i frankly admit that there is also 'head' invoved ;)
    >
    > i really have no idea -- but i always thought that these coreutils and
    > colleagues do their jobs as fast as possible, in particular faster than
    > interpreted languages... however, as i posted last time, i was actually
    > not aware that you have to call three of them.
    >
    > sebastian.

    I don't have the original post to know exactly what is needed, but looks like
    something that can be done by a single sed script.

    -Chetan
    Chetan, Oct 26, 2006
    #8
  9. Chetan wrote:
    > Sebastian Busch <> writes:
    >
    >> Steve Holden wrote:
    >>> Sebastian Busch wrote:
    >>>> wrote:
    >>>>> ... I would like to remove two lines from a file. ...
    >>>> ... grep -v ...
    >>> ... show ...

    >> grep -v "`grep -v "commentsymbol" yourfile | head -2`" yourfile
    >> ...

    > I don't have the original post to know exactly what is needed, but looks like
    > something that can be done by a single sed script.
    >
    > -Chetan


    Hey!

    The task is:

    "Remove the first two lines that don't begin with "@" from a file."


    Actually, the grep-thing I offered will also delete copies of these two
    lines that occur in another place. That should be no problem if the file
    is something like

    @comment
    deleteme
    deleteme
    @comment
    data: x-y-dy

    However, if this is not the case, it cannot be done this way.
    How would you do it with sed?

    Best,
    Sebastian.
    Sebastian Busch, Oct 26, 2006
    #9
  10. Sebastian Busch wrote:
    > The task is:
    >
    > "Remove the first two lines that don't begin with "@" from a file."


    awk 'BEGIN {c = 0} c < 2 && !/^@/ {c += 1; next} {print}' < mybeautifulfile

    --
    Roberto Bonvallet
    Roberto Bonvallet, Oct 26, 2006
    #10
  11. John Savage Guest

    Sebastian Busch <> writes:
    >The task is:
    >
    >"Remove the first two lines that don't begin with "@" from a file."
    >
    >How would you do it with sed?


    Why a sed solution in a python group?

    sed '/^@/!{G;/\n\n\n/{P;d;};s/[^\n]*//;h;d;}' data
    --
    John Savage (my news address is not valid for email)
    John Savage, Nov 5, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Wright
    Replies:
    0
    Views:
    507
    Joe Wright
    Jul 27, 2003
  2. Murali
    Replies:
    2
    Views:
    558
    Jerry Coffin
    Mar 9, 2006
  3. Umesh
    Replies:
    8
    Views:
    456
    Charlton Wilbur
    May 30, 2007
  4. Matt Coffman

    file compare and remove lines

    Matt Coffman, Aug 22, 2006, in forum: Ruby
    Replies:
    13
    Views:
    228
    Steve Litt
    Aug 25, 2006
  5. Replies:
    7
    Views:
    473
    Peter J. Acklam
    Sep 12, 2005
Loading...

Share This Page