Re: Delete lines containing a specific word

Discussion in 'Python' started by Francesco Pietra, Jan 6, 2008.

  1. Steven:
    Thanks. See below please (of very marginal interest)

    --- Steven D'Aprano <> wrote:

    > On Sun, 06 Jan 2008 09:21:33 -0800, Francesco Pietra wrote:
    >
    > > Please, how to adapt the following script (to delete blank lines) to
    > > delete lines containing a specific word, or words?

    >
    > That's tricky, because deleting lines from a file isn't a simple
    > operation. No operating system I know of (Windows, Linux, OS X) has a
    > "delete line" function.


    As I am at Debian Linux, I do that with grep -v


    >
    > Do you really need to delete the lines in place? It would be much simpler
    > to leave the original data as-is, and create a new file with just the
    > lines that aren't deleted.
    >
    >
    > > f=open("output.pdb", "r")
    > > for line in f:
    > > line=line.rstrip()
    > > if line:
    > > print line
    > > f.close()

    >
    > How to adapt this script:
    >
    > First, think about what this script does. That is, it goes through each
    > line, and if the line is not blank, it prints it.
    >
    > What do you want it to do instead? You want it to print the line if the
    > line doesn't contain a specific word. So that's the first thing you need
    > to change.
    >
    > Secondly, you might want the script to write its output to a file,
    > instead of printing. So, instead of the line "print line", you want it to
    > write to a file.


    may be cumbersome, though I use 2>&1 | tee output file.pdb so that I can see
    what happens on the screen and have the modified file.

    >
    > Before you can write to a file, you need to open it. So you will need to
    > open another file: you will have two files open, one for input and one
    > for output. And you will need to close them both when you are finished.
    >
    > Does that help you to adapt the script?
    >
    >
    > > If python in Linux accepts lines beginning with # as comment lines,
    > > please also a script to comment lines containing a specific word, or
    > > words, and back, to remove #.

    >
    > The same process applies. Instead of "delete line", you want to "comment
    > line".
    >
    >
    >
    > --
    > Steven
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    ____________________________________________________________________________________
    Be a better friend, newshound, and
    know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
    Francesco Pietra, Jan 6, 2008
    #1
    1. Advertising

  2. On Sun, 06 Jan 2008 13:33:52 -0800, Francesco Pietra wrote:

    > Steven:
    > Thanks. See below please (of very marginal interest)
    >
    > --- Steven D'Aprano <> wrote:
    >
    >> On Sun, 06 Jan 2008 09:21:33 -0800, Francesco Pietra wrote:
    >>
    >> > Please, how to adapt the following script (to delete blank lines) to
    >> > delete lines containing a specific word, or words?

    >>
    >> That's tricky, because deleting lines from a file isn't a simple
    >> operation. No operating system I know of (Windows, Linux, OS X) has a
    >> "delete line" function.

    >
    > As I am at Debian Linux, I do that with grep -v


    grep doesn't delete lines. grep matches lines. If you want to delete
    them, you still have to do the rest of the job yourself.


    >> Secondly, you might want the script to write its output to a file,
    >> instead of printing. So, instead of the line "print line", you want it
    >> to write to a file.

    >
    > may be cumbersome, though I use 2>&1 | tee output file.pdb so that I
    > can see what happens on the screen and have the modified file.


    Yes, matching lines and sending them to stdout is a better solution than
    trying to delete them from a file.



    --
    Steven
    Steven D'Aprano, Jan 6, 2008
    #2
    1. Advertising

  3. Steven D'Aprano wrote:

    > grep doesn't delete lines. grep matches lines. If you want to
    > delete them, you still have to do the rest of the job yourself.


    In which way does "grep -v mypattern myfile > myfile" not delete the
    lines matching mypattern?

    Regards,


    Björn

    --
    BOFH excuse #184:

    loop found in loop in redundant loopback
    Bjoern Schliessmann, Jan 6, 2008
    #3
  4. Francesco Pietra

    bukzor Guest

    On Jan 6, 3:33 pm, Bjoern Schliessmann <usenet-
    > wrote:
    > Steven D'Aprano wrote:
    > > grep doesn't delete lines. grep matches lines. If you want to
    > > delete them, you still have to do the rest of the job yourself.

    >
    > In which way does "grep -v mypattern myfile > myfile" not delete the
    > lines matching mypattern?
    >
    > Regards,
    >
    > Björn
    >
    > --
    > BOFH excuse #184:
    >
    > loop found in loop in redundant loopback


    If you do that, you'll find an empty file at the end.
    bukzor, Jan 6, 2008
    #4
  5. On 2008-01-06, Steven D'Aprano <> wrote:
    > On Sun, 06 Jan 2008 13:33:52 -0800, Francesco Pietra wrote:
    >
    >> Steven:
    >> Thanks. See below please (of very marginal interest)
    >>
    >> --- Steven D'Aprano <> wrote:
    >>
    >>> On Sun, 06 Jan 2008 09:21:33 -0800, Francesco Pietra wrote:
    >>>
    >>> > Please, how to adapt the following script (to delete blank lines) to
    >>> > delete lines containing a specific word, or words?
    >>>
    >>> That's tricky, because deleting lines from a file isn't a simple
    >>> operation. No operating system I know of (Windows, Linux, OS X) has a
    >>> "delete line" function.

    >>
    >> As I am at Debian Linux, I do that with grep -v

    >
    > grep doesn't delete lines. grep matches lines.


    grep does far more than that.

    > If you want to delete them, you still have to do the rest of
    > the job yourself.


    Nonsense.

    How is this not doing what the OP asks?

    grep -v pattern infile >outfile; mv outfile infile

    If you don't like explicitly using a second file, you can use
    sed:

    sed -i '/pattern/d' filename

    >>> Secondly, you might want the script to write its output to a file,
    >>> instead of printing. So, instead of the line "print line", you want it
    >>> to write to a file.

    >>
    >> may be cumbersome, though I use 2>&1 | tee output file.pdb so that I
    >> can see what happens on the screen and have the modified file.

    >
    > Yes, matching lines and sending them to stdout is a better
    > solution than trying to delete them from a file.


    If you're matching all lines that don't contain the pattern in
    question, then matching all lines and sending them to stdout
    _is_ a way to delete them.

    --
    Grant Edwards grante Yow! And furthermore,
    at my bowling average is
    visi.com unimpeachable!!!
    Grant Edwards, Jan 7, 2008
    #5
  6. On Mon, 07 Jan 2008 00:33:36 +0100, Bjoern Schliessmann wrote:

    > Steven D'Aprano wrote:
    >
    >> grep doesn't delete lines. grep matches lines. If you want to delete
    >> them, you still have to do the rest of the job yourself.

    >
    > In which way does "grep -v mypattern myfile > myfile" not delete the
    > lines matching mypattern?


    Okay, that will delete the lines matching mypattern. Unfortunately it
    will also delete all the lines NOT matching mypattern as well. Try it for
    yourself -- just not on anything you care about.

    This is what happens when abstractions leak. You *think* you're deleting
    lines, but you're not. That's just an abstraction, and when it leaks, you
    break things.



    --
    Steven
    Steven D'Aprano, Jan 7, 2008
    #6
  7. On Mon, 07 Jan 2008 00:42:01 +0000, Grant Edwards wrote:

    >> If you want to delete them, you still have to do the rest of the job
    >> yourself.

    >
    > Nonsense.
    >
    > How is this not doing what the OP asks?
    >
    > grep -v pattern infile >outfile; mv outfile infile


    It isn't deleting lines. As abstractions go, it comes pretty close, but
    just try it on a disk with insufficient free space for the temporary
    outfile and watch it break.


    --
    Steven
    Steven D'Aprano, Jan 7, 2008
    #7
    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. Murali
    Replies:
    2
    Views:
    542
    Jerry Coffin
    Mar 9, 2006
  2. Francesco Pietra

    Delete lines containing a specific word

    Francesco Pietra, Jan 6, 2008, in forum: Python
    Replies:
    3
    Views:
    760
  3. Matt Nordhoff

    Re: Delete lines containing a specific word

    Matt Nordhoff, Jan 6, 2008, in forum: Python
    Replies:
    1
    Views:
    321
    Grant Edwards
    Jan 6, 2008
  4. Francesco Pietra

    Fwd: Delete lines containing a specific word

    Francesco Pietra, Jan 6, 2008, in forum: Python
    Replies:
    0
    Views:
    248
    Francesco Pietra
    Jan 6, 2008
  5. Eden Cardim
    Replies:
    11
    Views:
    191
    Eden Cardim
    Mar 13, 2006
Loading...

Share This Page