Delete lines containing a specific word

  • Thread starter Francesco Pietra
  • Start date
F

Francesco Pietra

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/newsearch/category.php?category=shopping
 
S

Steven D'Aprano

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".
 
D

Dennis Lee Bieber

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.
How I miss the old SDS/XDS CP/V <G>

It did have a sort of delete-line if the text file had ever been run
through the normal editor... That's because the OS itself offered
transparent support for three file allocation/formats: consecutive
(essentially the common UNIX/Windows new-line formatted stream; could be
scattered across the disk space); keyed (ISAM keyed file, the default
key used by the editor being numeric, and mapped by FORTRAN IV into
direct access "record number"); and random (being a /contiguous/ disk
allotment with NO implied internal structure; fixed size upon creation
[or fail if the disk is too fragmented]).

So "delete-line" on a keyed/editor text file was possible, but might
lead to a need to compress/reindex the file at some point in time to
reclaim wasted space.


Of course, this was also the OS that had four file modes: read,
write, update (must read before writing), and scratch (must write before
reading) which could be applied to consecutive (stream) files. Update
and scratch both maintained two file position pointers, one for read
operations, one for write operations -- no need to do tell()/seek()
operations when switching from read to write; just had to ensure that
(for update) you have read the same or more than you plan to write.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
M

mik3l3374

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/newsearch/category.php?category=shopping

for line in open("file"):
if not "word" in line:
print line


on the command, use the ">" operator to redirect to a file
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top