471,310 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Delete lines containing a specific word

Please, how to adapt the following script (to delete blank lines) to delete
lines containing a specific word, or words?

f=open("output.pdb", "r")
for line in f:
line=line.rstrip()
if line:
print line
f.close()

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 #.

Thanks
francesco pietra
__________________________________________________ __________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping
Jan 6 '08 #1
2 19480
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.

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.

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
Jan 6 '08 #2
On Jan 7, 1:21 am, Francesco Pietra <chiendar...@yahoo.comwrote:
Please, how to adapt the following script (to delete blank lines) to delete
lines containing a specific word, or words?

f=open("output.pdb", "r")
for line in f:
line=line.rstrip()
if line:
print line
f.close()

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 #.

Thanks
francesco pietra

__________________________________________________ __________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping
for line in open("file"):
if not "word" in line:
print line
on the command, use the ">" operator to redirect to a file
Jan 7 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Francesco Pietra | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.